我有以下Json字符串(包含subArray):
{ “属性”: [{ “名称”: “一”, “数据”:[ “10”, “0”, “50”], “数据名”:[ “2000248”, “2789290”, “2789291”], “dataValue”:[ “一”, “B”, “C”], “toClick”: “d”}, { “名称”: “V”, “数据”:[ “0”, “0”, “0”, “20”] “数据名”: [ “49500000”, “49500001”, “49500002”, “49500003”], “dataValue”:[ “一”, “V”, “V”, “d”], “toClick”: “d”}]}“
我无法在csharp中反序列化。 [评论后更新] 我做了什么:
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()
我该怎么办?
答案 0 :(得分:2)
根据您的JSON,DataJsonAttributeContainer
中的您的属性名称错误。这是一个尽我所知的例子:
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
}
}
答案 1 :(得分:0)
您必须指定正确的类型,如果是DataJsonAttributeContainer
,则必须指定正确的属性名称:
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; }
}
使用之前的定义,以下内容适合我(在我从JSON字符串中删除最后一个"
之后):
var data = JsonConvert.DeserializeObject<DataJsonAttributeContainer>(jsonString);