I have the following Json string (containing subArray):
{"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"}]}"
I can't deserialize in csharp. [Updated after comment] What I did:
public class attributes
{
public string name { get; set; }
开发者_JAVA百科 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);
}
Test method:
testJson()
What can I do?
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
}
}
You have to specify the correct types and, in the case of your DataJsonAttributeContainer
, the correct property name too:
public class attributes
{
public string name { get; set; }
public int[] data { get; set; }
public int[] dataName { get; set; }
public string[] dataValue { get; set; }
public string toClick { get; set; }
}
public class DataJsonAttributeContainer
{
public List<attributes> attributes { get; set; }
}
With the previous definitions, the following works for me (after I removed the last "
from your JSON string):
var data = JsonConvert.DeserializeObject<DataJsonAttributeContainer>(jsonString);
精彩评论