如何在c#中反序列化json数组 [英] How to deserialize json array in c#

查看:125
本文介绍了如何在c#中反序列化json数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个JSON数据:



 {case_statuses:{:选择案例状态,1 :1-Request Initiated,2:2-In Progress,3:3-Pending Internal,4:4-Pending External,5:5-Cancelled ,6:6-Closed},case_types:{:选择案例类型,9:管理,1:民事,3:商业 ,2:犯罪,12:专家案例,10:IP,5:人工,4:其他,13:Outlook-Add -In,6:房地产,8:租金争议,7:Sharia \ / Legacy},provider_groups:{: - ,1 :法律部,2:法律团队1,3:法律团队2}} 





我怎样才能反序列化?

解决方案





使用JSON.Net链接:< a href =http://james.newtonking.com/json> http://james.newtonking.com/json [ ^ ]



如何使用JSON.Net对JSON进行脱盐



<前lang =cs> string json = @ {
'Name' :'Bad Boys',
'ReleaseDate':'1995-4-7T00:00:00',
'Grares':[
'Action',
'Comedy'
]
}
;

电影m = JsonConvert.DeserializeObject< Movie>(json);

string name = m.Name;





这个例子你可以在上面提到的链接找到。



但现在非常小心JSON中的文化信息。如果您使用相同的应用程序进行序列化和反序列化,那么这不是问题,但如果没有则提供其他设置。



  //  序列化 
TestType ObjGraph = new TestType( );
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(ObjGraph, typeof (TestType), new Newtonsoft.Json.JsonSerializerSettings()
{
DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat,
DateTimeZoneHandling = Newtonsoft.Json .DateTimeZoneHandling.Local,
});

// 反序列化
TestType Obj = ; // = default(T);

Obj = Newtonsoft.Json。 JsonConvert.DeserializeObject(jsonString, typeof (TestType),
new Newtonsoft.Json.JsonSerializerSettings( )
{
DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat,
DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local

});





享受:)


示例: HERE [ ^ ]或 HERE [ ^ ]。


请参阅我最近的回答: 如何将对象类型转换为C#类对象类型 [ ^ ]。



还有这一个,基于以上内容:如何将多级json数据转换为C#对象? [ ^ ]。



-SA


I have this JSON Data :

{"case_statuses":{"":"Choose Case Status","1":"1-Request Initiated","2":"2-In Progress","3":"3-Pending Internal","4":"4-Pending External","5":"5-Cancelled","6":"6-Closed"},"case_types":{"":"Choose Case Type","9":"Administrative","1":"Civil","3":"Commercial","2":"Criminal","12":"Expert Case","10":"IP","5":"Labor","4":"Other","13":"Outlook-Add-In","6":"Real Estate","8":"Rent Dispute","7":"Sharia\/Legacy"},"provider_groups":{"":"--","1":"Legal Department","2":"Legal Team 1","3":"Legal Team 2"}}



How I can deserialize this please ?

解决方案

Hi,

Use JSON.Net Link : http://james.newtonking.com/json[^]

How to desalinize JSON using JSON.Net

string json = @"{
  'Name': 'Bad Boys',
  'ReleaseDate': '1995-4-7T00:00:00',
  'Genres': [
    'Action',
    'Comedy'
  ]
}";

Movie m = JsonConvert.DeserializeObject<Movie>(json);

string name = m.Name;



This example you will find at above mentioned link.

but now very careful about culture information in JSON. If you serialize and Deserialize with same application then that's not a problem but if not then provide additional settings.

//Serialize 
TestType ObjGraph=new TestType();
string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(ObjGraph,typeof(TestType), new Newtonsoft.Json.JsonSerializerSettings()
            {
                DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat,
                DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local,
            });

//Deserialize
TestType Obj = null; //= default(T);

            Obj = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonString , typeof(TestType),
                new Newtonsoft.Json.JsonSerializerSettings()
                {
                    DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat,
                    DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local
                   
                });



Enjoy :)


example: HERE[^] or HERE[^].


Please see my recent answer: How To Convert object type to C# class object type[^].

And also this one, based on the above: how to conver multi level json data to C# Object?[^].

—SA


这篇关于如何在c#中反序列化json数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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