如何将Json字符串转换为对象列表 [英] How to convert Json string to list of objects

查看:107
本文介绍了如何将Json字符串转换为对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我有一个JSON字符串。我需要将其转换为对象列表。我怎么能这样做?

我为此创建了一个类。下面是细节。请帮助

  string  Restresponse =  string  .Empty; 
Restresponse = reader.ReadToEnd();
JObject json = JObject.Parse(Restresponse);





  class 计划
{
public string AssumptionsOverrides { get ; set ; }
public string ChannelId { get ; set ; }
public string 客户端{ get ; set ; }
public string CreatedDate { get ; set ; }
public string CustomAssetClasses { get ; set ; }
public string FaNumber { get ; set ; }
public string Href { get ; set ; }
public string LastAnalysisDate { get ; set ; }
public string 名称{ get ; set ; }
public string OfficeNumber { get ; set ; }
public string PlanId { get ; set ; }
public string 场景{ get ; set ; }
public string 设置{ get ; set ; }
public string UpdatedDate { get ; set ; }
public string VerificationTimestamp { get ; set ; }

}





 {
plans:[{
planId 10001
name 我的客户计划1
href null
channelId WSGPLN
officeNumber 101
faNumber 195
createdDate 2016-01-27T15:43:50
updatedDate 2016-01-27T15:43:50
lastAnalysisDate null
clients null
scenario null
settings null
assumptionsOverrides null
customAssetClasses null
< span class =code-string> verificationTimestamp 2016-01-27T15:43:50
},
{
planId 10025
name 我的客户计划25
href null
channelId WSGPLN
officeNumber 101
faNumber 195
createdDate 2016-01-27T15:43:50
updatedDate 2016- 01-27T15:43:50
lastAnalysisDate:< span class =code-keyword> null ,
clients null
scenario null
settings null
assumptionsOverrides null
customAssetClasses null
verificationTimestamp 2016-01-27T15:43:50
}],
metadata:{
totalCount 25
offset null
limit null
}
}

解决方案

您拥有的JSON与您尝试反序列化的类不匹配。您的JSON是一个名为plans的属性,它是一个具有名为planId等属性的对象数组。您要反序列化的类是一个具有名为PlanID等属性的对象。当您将该JSON反序列化为该类时,它将看看把这个集合放到一个名为plans的array \ list属性中,但你的Plan类没有这样的属性。



你将不得不改变JSON或改变你的课程。此代码使用System.Runtime.Serialization类,它还需要您向类添加一些属性,不仅仅是为了允许它们被序列化,而且还要处理JSON中属性的情况。不能在你的课上匹配(我在PlanID和Name上做了这个,给你一个例子并测试它是否有效,你需要自己做其他人)。如果你使用像已经提到的Newtonsoft那样的库,我敢说你不会有这些繁琐的问题,你将能够使用你的课而不需要特别的注释,我只是使用这种方法,因为它的工作原理盒子。



更新的课程



 [DataContract] 
class 计划
{
[DataMember]
public 列表与LT;计划和GT;计划{获取; set ; }
}

[DataContract]
class 计划
{
[DataMember]
public string AssumptionsOverrides { get ; set ; }
[DataMember]
public string ChannelId {获得; set ; }
[DataMember]
public string 客户端{获得; set ; }
[DataMember]
public string CreatedDate {获得; set ; }
[DataMember]
public string CustomAssetClasses {获得; set ; }
[DataMember]
public string FaNumber {获得; set ; }
[DataMember]
public string Href {获得; set ; }
[DataMember]
public string LastAnalysisDate {获得; set ; }
[DataMember(Name = name)]
public string 名称{ get ; set ; }
[DataMember]
public string OfficeNumber {获得; set ; }
[DataMember(Name = planId)]
public string PlanId { get ; set ; }
[DataMember]
public string 场景{获得; set ; }
[DataMember]
public string 设置{获得; set ; }
[DataMember]
public string UpdatedDate {获得; set ; }
[DataMember]
public string VerificationTimestamp {获得; set ; }

}





使用



  string  json =   {\plans \:[{....; 

MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(json));
ms.Position = 0 ;

// 需要引用System.Runtime.Serialization
DataContractJsonSerializer ser = new DataContractJsonSerializer( typeof (Plans));
计划p =(计划)ser.ReadObject(ms);


可能是在.NET中处理JSON的最佳库...

Json.NET - Newtonsoft [ ^ ]


你可以试试这种方式



  public   class 计划
{
< span class =code-keyword> public int planId { get ; set ; }
public string name { get ; set ; }
public object href { get ; set ; }
public string channelId { get ; set ; }
public int officeNumber { get ; set ; }
public int faNumber { get ; set ; }
public string createdDate { get ; set ; }
public string updatedDate { get ; set ; }
public object lastAnalysisDate { get ; set ; }
public object clients { get ; set ; }
public object scenario { get ; set ; }
public object settings { get ; set ; }
public object assumptionsOverrides { get ; set ; }
public object customAssetClasses { get ; set ; }
public string verificationTimestamp { get ; set ; }
}

public class 元数据
{
public int totalCount { get ; set ; }
public object offset { get ; set ; }
public object limit { get ; set ; }
}

public class RootObject
{
public 列表<计划>计划{获取; set ; }
public 元数据元数据{ get ; set ; }
}
// c#c​​onvert to objects
string json = 你的jsonstring;

RootObject rootObject = new JavaScriptSerializer()。反序列化< rootobject>(json);

< / rootobject >


Hi Friends,

i have a JSON string. I need to convert it into list of objects. How can i do this?
I created a class for this.Below are the details. please help

string Restresponse = string.Empty;
Restresponse = reader.ReadToEnd();
JObject json = JObject.Parse(Restresponse);



class Plans
   {
       public string AssumptionsOverrides { get; set; }
       public string ChannelId { get; set; }
       public string Clients { get; set; }
       public string CreatedDate { get; set; }
       public string CustomAssetClasses { get; set; }
       public string FaNumber { get; set; }
       public string Href { get; set; }
       public string LastAnalysisDate { get; set; }
       public string Name { get; set; }
       public string OfficeNumber { get; set; }
       public string PlanId { get; set; }
       public string Scenarios { get; set; }
       public string Settings { get; set; }
       public string UpdatedDate { get; set; }
       public string VerificationTimestamp { get; set; }

   }



{
	"plans": [{
		"planId": 10001,
		"name": "My Client Plan 1",
		"href": null,
		"channelId": "WSGPLN",
		"officeNumber": 101,
		"faNumber": 195,
		"createdDate": "2016-01-27T15:43:50",
		"updatedDate": "2016-01-27T15:43:50",
		"lastAnalysisDate": null,
		"clients": null,
		"scenarios": null,
		"settings": null,
		"assumptionsOverrides": null,
		"customAssetClasses": null,
		"verificationTimestamp": "2016-01-27T15:43:50"
	},
{
		"planId": 10025,
		"name": "My Client Plan 25",
		"href": null,
		"channelId": "WSGPLN",
		"officeNumber": 101,
		"faNumber": 195,
		"createdDate": "2016-01-27T15:43:50",
		"updatedDate": "2016-01-27T15:43:50",
		"lastAnalysisDate": null,
		"clients": null,
		"scenarios": null,
		"settings": null,
		"assumptionsOverrides": null,
		"customAssetClasses": null,
		"verificationTimestamp": "2016-01-27T15:43:50"
	}],
	"metadata": {
		"totalCount": 25,
		"offset": null,
		"limit": null
	}
}

解决方案

The JSON you have doesn't match the class you are trying to deserialise to. Your JSON is a property called "plans" that is an array of objects that have a property called planId etc. The class you are deserialising to is an object that has a property called PlanID etc. When you deserialise that JSON into that class it will look to put the collection into an array\list property called plans but your Plan class has no such property.

You're going to have to either change the JSON or change your classes. This code uses the System.Runtime.Serialization classes, which will also need you to add some attributes to your classes, not just to allow them to be serialised, but also to take care of the fact that the case of property in your JSON doesn't match that on your class (I've done this on PlanID and Name to give you an example and to test it works, you'd need to do the others yourself). If you use a library like the Newtonsoft one already mentioned I dare say you won't have these fiddly issues, you'll be able to use your classes without bothering about special annotations, I'm only using this method as it works out of the box.

The updated classes

[DataContract]
class Plans
{
    [DataMember]
    public List<Plan> plans { get; set; }
}

[DataContract]
class Plan
{
    [DataMember]
    public string AssumptionsOverrides { get; set; }
    [DataMember]
    public string ChannelId { get; set; }
    [DataMember]
    public string Clients { get; set; }
    [DataMember]
    public string CreatedDate { get; set; }
    [DataMember]
    public string CustomAssetClasses { get; set; }
    [DataMember]
    public string FaNumber { get; set; }
    [DataMember]
    public string Href { get; set; }
    [DataMember]
    public string LastAnalysisDate { get; set; }
    [DataMember(Name="name")]
    public string Name { get; set; }
    [DataMember]
    public string OfficeNumber { get; set; }
    [DataMember(Name="planId")]
    public string PlanId { get; set; }
    [DataMember]
    public string Scenarios { get; set; }
    [DataMember]
    public string Settings { get; set; }
    [DataMember]
    public string UpdatedDate { get; set; }
    [DataMember]
    public string VerificationTimestamp { get; set; }

}



Usage

string json = "{\"plans\": [{.... ";

MemoryStream ms = new MemoryStream(System.Text.ASCIIEncoding.ASCII.GetBytes(json));
ms.Position = 0;

// Needs a reference to "System.Runtime.Serialization"
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Plans));
Plans p = (Plans) ser.ReadObject(ms);


Probably the best library to handle JSON in .NET...
Json.NET - Newtonsoft[^]


you can try this way

public class Plan
{
    public int planId { get; set; }
    public string name { get; set; }
    public object href { get; set; }
    public string channelId { get; set; }
    public int officeNumber { get; set; }
    public int faNumber { get; set; }
    public string createdDate { get; set; }
    public string updatedDate { get; set; }
    public object lastAnalysisDate { get; set; }
    public object clients { get; set; }
    public object scenarios { get; set; }
    public object settings { get; set; }
    public object assumptionsOverrides { get; set; }
    public object customAssetClasses { get; set; }
    public string verificationTimestamp { get; set; }
}

public class Metadata
{
    public int totalCount { get; set; }
    public object offset { get; set; }
    public object limit { get; set; }
}

public class RootObject
{
    public List<Plan> plans { get; set; }
    public Metadata metadata { get; set; }
}
//c# convert to objects
 string json = "your jsonstring";
            
            RootObject rootObject = new JavaScriptSerializer().Deserialize<rootobject>(json);

</rootobject>


这篇关于如何将Json字符串转换为对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆