在JSON中反序列化嵌套数组中的数组

huangapple go评论68阅读模式
英文:

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

{
&quot;InputFileFolder&quot;: &quot;D:\\MontlyTransactions\\&quot;,
&quot;TransactionGroups&quot;:
[
	{
		&quot;Name&quot;: &quot;Income&quot;, 
		&quot;FilterString&quot;: &quot;Type=@0 and Amount&gt;@1 and (Description.Contains(@2) or Description.Contains(@3))&quot;,
		&quot;Arguments&quot;: [&quot;BAC&quot;,0,&quot;REF1&quot;,&quot;REF2&quot;]
	},
	{
		&quot;Name&quot;: &quot;Household Bills&quot;, 
		&quot;FilterString&quot;: &quot;Type=@0 and Amount&lt;@1 and Amount.Contains(@2)&quot;,
		&quot;Arguments&quot;: [&quot;S/O&quot;,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&lt;Configuration&gt;(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

 &#39;Arguments&#39;: [&#39;BAC&#39;,0,&#39;REF1&#39;,&#39;REF2&#39;]   

and

 &#39;Arguments&#39;: [&#39;S/O&#39;,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。但是,您可以检查类型并使用 JArrayToObject 方法将其转换回 object[]string[],如下所示:

foreach (var tg in result.TransactionGroups)
{
    for (var i = 0; i &lt; tg.Arguments.Length; i++)
    {
        var arrArg = tg.Arguments[i];
        if (arrArg is JArray realArr)
        {
            tg.Arguments[i] = realArr.ToObject&lt;string[]&gt;();
        }
    }
}

Fiddle


<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 &lt; tg.Arguments.Length; i++)
			{
				var arrArg = tg.Arguments[i];
				if (arrArg is JArray realArr)
				{
					tg.Arguments[i] = realArr.ToObject&lt;string[]&gt;();
				}
			}
		}

Fiddle

huangapple
  • 本文由 发表于 2020年1月3日 20:07:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/59578380.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定