英文:
Deserialize an array within an array in JSON
问题
我有以下的JSON文件,我想要将其反序列化为一个对象:
{
"InputFileFolder": "D:\\MontlyTransactions\\",
"TransactionGroups":
[
{
"Name": "Income",
"FilterString": "Type=@0 and Amount>@1 and (Description.Contains(@2) or Description.Contains(@3))",
"Arguments": ["BAC", 0, "REF1", "REF2"]
},
{
"Name": "Household Bills",
"FilterString": "Type=@0 and Amount<@1 and Amount.Contains(@2)",
"Arguments": ["S/O", 0, [16600, 72000, 15000]]
}
]
}
我试图转换的对象如下所示:
public class Configuration
{
public string InputFileFolder { get; set; }
public TransactionGroup[] TransactionGroups { get; set; }
}
public class TransactionGroup
{
public string Name { get; set; }
public string FilterString { get; set; }
public string[] Arguments { get; set; }
}
使用以下代码段:
this.Configuration = JsonConvert.DeserializeObject<Configuration>(configurationString);
然而,第二个参数内的数组被返回为Newtonsoft.Json.Linq.Array,而我原本期望的是一个object[]。
任何建议将不胜感激。
英文:
I have the following JSON file that I want to deserialize into an object
{
"InputFileFolder": "D:\\MontlyTransactions\\",
"TransactionGroups":
[
{
"Name": "Income",
"FilterString": "Type=@0 and Amount>@1 and (Description.Contains(@2) or Description.Contains(@3))",
"Arguments": ["BAC",0,"REF1","REF2"]
},
{
"Name": "Household Bills",
"FilterString": "Type=@0 and Amount<@1 and Amount.Contains(@2)",
"Arguments": ["S/O",0,[16600,72000,15000]]
}
]
}
The object I am trying to convert to is as below:
public class Configuration
{
public string InputFileFolder { get; set; }
public TransactionGroup[] TransactionGroups { get; set; }
}
public class TransactionGroup
{
public string Name { get; set; }
public string FilterString { get; set; }
public string[] Arguments { get; set; }
}
using the below snippet:
this.Configuration = JsonConvert.DeserializeObject<Configuration>(configurationString);
However the array within the second argument is returned as a Newtonsoft.Json.Linq.Array instead of an object[] which is what I was expecting.
Any suggestions would be hugely appreciated.
答案1
得分: 0
你需要从Json中检查参数元素的内容。例如,从OP提供的Json中:
'Arguments': ['BAC', 0, 'REF1', 'REF2']
和
'Arguments': ['S/O', 0, [16600, 72000, 15000]]
正如你所看到的,涉及的元素不是字符串数组。实际上,它包含字符串、数字和数字的子数组。
所以你需要更改你的类定义如下:
public class TransactionGroup
{
public string Name { get; set; }
public string FilterString { get; set; }
public object[] Arguments { get; set; } // 在这里进行更改
}
这将帮助你成功反序列化Json。
英文:
You need to start by checking the Arguement elements in Json. For example, from the Json given in OP
'Arguments': ['BAC',0,'REF1','REF2']
and
'Arguments': ['S/O',0,[16600,72000,15000]]
As you can observe the, elements involved are not an array of strings. In fact, it contains strings, numbers and sub arrays of numbers as well.
So you need to change your class definition as
public class TransactionGroup
{
public string Name { get; set; }
public string FilterString { get; set; }
public object[] Arguments { get; set; } // Change here
}
That would help you deserialize the Json successfully.
答案2
得分: 0
即使将数据类型更改为:public object[] Arguments { get; set; }
,它仍然会被序列化为 JArray
。但是,您可以检查类型并使用 JArray
的 ToObject
方法将其转换回 object[]
或 string[]
,如下所示:
foreach (var tg in result.TransactionGroups)
{
for (var i = 0; i < tg.Arguments.Length; i++)
{
var arrArg = tg.Arguments[i];
if (arrArg is JArray realArr)
{
tg.Arguments[i] = realArr.ToObject<string[]>();
}
}
}
<details>
<summary>英文:</summary>
Even if you change your data type to : `public object[] Arguments { get; set; }` it will be serialized as `JArray`. But you can check the type and convert back into `object[]` or `string[]` with using `ToObject` method of `JArray` like below :
```csharp
foreach (var tg in result.TransactionGroups)
{
for (var i = 0; i < tg.Arguments.Length; i++)
{
var arrArg = tg.Arguments[i];
if (arrArg is JArray realArr)
{
tg.Arguments[i] = realArr.ToObject<string[]>();
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论