简体   繁体   English

DataContractJsonSerializer用于反序列化JSON

[英]DataContractJsonSerializer to deserialize JSON

I have a Json string which looks like below, 我有一个如下所示的Json字符串,

{
    "ErrorDetails":null,
    "Success":true,
    "Records":[
                {
                "Attributes":[
                                {
                                    "Name":"accountid",
                                    "Value":null
                                },
                                {
                                    "Name":"accountidname",
                                    "Value":null
                                }
                ],
                "Id":"9c5071f7-e4a3-e111-b4cc-1cc1de6e4b49",
                "Type":"contact"
                }
    ]
}

I'm using following to deserialize this string, 我正在使用以下方法反序列化此字符串,

DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(JSONPackage));
object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
JSONPackage jsonResponse = objResponse as JSONPackage;

And my JSONPackage looks like following, 我的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; }
    }
}

And Records looks like this, 唱片看起来像这样,

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 Record
    {
        public List<Attributes> Attributes { get; set; }
        public string Id { get; set; }
        public string Type { get; set; }
    }
}

Attributes looks like, 属性看起来像

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 Attributes
    {
        public AttributeItem AttributeItem { get; set; }
    }
}

And lastly AttributeItem looks like the following, 最后,AttributeItem如下所示:

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; }
    }
}

However, this doesn't seems to work. 但是,这似乎不起作用。

when I do, 当我做,

Console.WriteLine(jp.Records[0].Attributes[0].AttributeItem.Name);

I get a NullPointerException ( jp is a JSONPackage Object). 我得到一个NullPointerException( jp是一个JSONPackage对象)。

But, if I do, 但是,如果我这样做,

Console.WriteLine(jp.Records[0].Attributes.Count) i get "2"

Can you please assist? 你能帮忙吗?

You don't need the Attributes class. 您不需要Attributes类。

Change 更改

public List<Attributes> Attributes { get; set; }

to

public List<AttributeItem> Attributes { get; set; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用DataContractJsonSerializer反序列化嵌套的json? - How can I Deserialize nested json with DataContractJsonSerializer? 使用DataContractJsonSerializer使用不同类型的数组反序列化json - Deserialize json with array of different types using DataContractJsonSerializer WCF DataContractJsonSerializer 如何反序列化 json DateTime? - How does WCF DataContractJsonSerializer deserialize a json DateTime? 在Windows Phone中使用DataContractJsonSerializer将JSON反序列化为字典 - Deserialize JSON to Dictionary with DataContractJsonSerializer in Windows Phone JSON数组从DataContractJsonSerializer方法反序列化为JsonConvert(newton.json)? - Json array deserialize from DataContractJsonSerializer method to JsonConvert(newton.json)? 尝试使用JSON.NET和DataContractJsonSerializer反序列化JSON失败 - Trying to deserialize JSON using JSON.NET and DataContractJsonSerializer fails 使用DataContractJsonSerializer在.net中反序列化json base64二进制文件 - deserialize json base64 binary in .net using DataContractJsonSerializer Newtonsoft Json将字典从DataContractJsonSerializer反序列化为键/值列表 - Newtonsoft Json Deserialize Dictionary as Key/Value list from DataContractJsonSerializer 如何使用JSON.NET或DataContractJsonSerializer反序列化未类型化的对象 - How to deserialize an untyped object using JSON.NET or DataContractJsonSerializer 使用DataContractJsonSerializer将Json反序列化为C#动态对象 - Deserialize Json into a C# dynamic object using DataContractJsonSerializer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM