反序列化包含子数组的Json对象 [英] Deserialize Json Object containing sub Array
问题描述
我有以下Json字符串(包含subArray):
I have the following Json string (containing subArray):
{属性": [{"name":"a","data":["10","0","50"],"dataName":["2000248","2789290","2789291"], "dataValue":["a","b","c"],"toClick":"d"}, {名称":"v",数据":["0","0","0","20"] ,"dataName": ["49500000","49500001","49500002","49500003"], "dataValue":["a","v","v","d"],"toClick":"d"}]}"
{"attributes": [{"name":"a","data":["10","0","50"],"dataName":["2000248","2789290","2789291"], "dataValue":["a","b","c"],"toClick":"d"}, {"name":"v","data":["0","0","0","20"] ,"dataName": ["49500000","49500001","49500002","49500003"], "dataValue":["a","v","v","d"],"toClick":"d"}]}"
我无法在csharp中反序列化. [评论后更新] 我做了什么:
I can't deserialize in csharp. [Updated after comment] What I did:
public class attributes
{
public string name { get; set; }
public string[] data { get; set; }
public string[] dataName { get; set; }
public string[] dataValue { get; set; }
public string toClick { get; set; }
}
public class DataJsonAttributeContainer
{
public List<attributes> JsonAttributeAfterSaves { get; set; }
}
public static T DeserializeFromJson<T>(string json)
{
T deserializedProduct = JsonConvert.DeserializeObject<T>(json, settings);
return deserializedProduct;
}
private void testJson()
{
string JsonStr =
"{\"attributes\":[{\"name\":\"a\",\"data\":[\"10\",\"0\",\"50\"],\"dataName\":[\"2000248\",\"2789290\",\"2789291\"],\"dataValue\":[\"a\",\"a\",\"d\"],\"toClick\":\"d\"},{\"name\":\"d\",\"data\":[\"0\",\"0\",\"0\",\"20\"],\"dataName\":[\"49500000\",\"49500001\",\"49500002\",\"49500003\"],\"dataValue\":[\"a\",\"a\",\"d\",\"d\"],\"toClick\":\"a\"}]}";
var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonStr);
}
测试方法:
testJson()
我该怎么办?
推荐答案
在给定JSON的情况下,DataJsonAttributeContainer
中的属性名称错误.据我所知,这是一个可行的示例:
Your property within DataJsonAttributeContainer
has the wrong name given your JSON. Here's an example which works as far as I can tell:
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Attributes
{
public string name { get; set; }
public string[] data { get; set; }
public string[] dataName { get; set; }
public string[] dataValue { get; set; }
public string toClick { get; set; }
}
public class DataJsonAttributeContainer
{
public List<Attributes> attributes { get; set; }
}
class Test
{
public static T DeserializeFromJson<T>(string json)
{
T deserializedProduct = JsonConvert.DeserializeObject<T>(json);
return deserializedProduct;
}
static void Main()
{
string JsonStr =
"{\"attributes\":[{\"name\":\"a\",\"data\":[\"10\",\"0\",\"50\"],\"dataName\":[\"2000248\",\"2789290\",\"2789291\"],\"dataValue\":[\"a\",\"a\",\"d\"],\"toClick\":\"d\"},{\"name\":\"d\",\"data\":[\"0\",\"0\",\"0\",\"20\"],\"dataName\":[\"49500000\",\"49500001\",\"49500002\",\"49500003\"],\"dataValue\":[\"a\",\"a\",\"d\",\"d\"],\"toClick\":\"a\"}]}";
var container = DeserializeFromJson<DataJsonAttributeContainer>(JsonStr);
Console.WriteLine(container.attributes.Count); // Prints 2
Console.WriteLine(container.attributes[0].data.Length); // Prints 3
}
}
这篇关于反序列化包含子数组的Json对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!