C# 用于反序列化JSON的DataContractJsonSerializer
我有一个Json字符串,如下所示C# 用于反序列化JSON的DataContractJsonSerializer,c#,json,C#,Json,我有一个Json字符串,如下所示 { "ErrorDetails":null, "Success":true, "Records":[ { "Attributes":[ { "Name":"accountid",
{
"ErrorDetails":null,
"Success":true,
"Records":[
{
"Attributes":[
{
"Name":"accountid",
"Value":null
},
{
"Name":"accountidname",
"Value":null
}
],
"Id":"9c5071f7-e4a3-e111-b4cc-1cc1de6e4b49",
"Type":"contact"
}
]
}
我正在使用以下命令反序列化此字符串
DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(JSONPackage));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
JSONPackage jsonResponse = objResponse as JSONPackage;
我的JSONPackage如下所示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CommonLibs.JSONObjects
{
public class JSONPackage
{
public string ErrorDetails { get; set; }
public string Success { get; set; }
public List<Record> Records { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace CommonLibs.JSONObjects
{
public class AttributeItem
{
public string Name { get; set; }
public string Value { get; set; }
}
}
最后,属性项如下所示
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CommonLibs.JSONObjects
{
public class JSONPackage
{
public string ErrorDetails { get; set; }
public string Success { get; set; }
public List<Record> Records { get; set; }
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace CommonLibs.JSONObjects
{
public class AttributeItem
{
public string Name { get; set; }
public string Value { get; set; }
}
}
然而,这似乎不起作用
当我这样做的时候
Console.WriteLine(jp.Records[0].Attributes[0].AttributeItem.Name);
我得到一个NullPointerException(jp
是一个JSONPackage对象)
但是,如果我这样做
Console.WriteLine(jp.Records[0].Attributes.Count) i get "2"
你能帮忙吗?你不需要
属性类
改变
公共列表属性{get;set;}
到
公共列表属性{get;set;}