反序列化json c# [英] deserialization json c#

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

问题描述

请帮助.我正在尝试反序列化json字符串(

 {"response":[259,{"body":ятебе","mid":17759,"uid":18537276,"from_id": 18537276,"date":1345466897,"read_state":1,"out":1},{"body":"jdjdjdjd","mid":17736,"uid":18537276,"from_id":18537276,"date :1345400400," read_state:1," out:1," attachment:{" type:" doc," doc:{" did:118620269," owner_id:18537276," title: "IMG_20120722_220434.jpg",大小":475878,"ext":"jpg","url":"http:\/\/vk.com \/doc18537276_118620269?hash = 59f1fe51a02a65519c& dl = 3972a9297f3da6a20c",拇指" :"http:\/\/cs4343.vkontakte.ru \/u18537276 \/-3 \/m_69a4a578da.jpg","access_key":"7a6cb6559b179dec16"}} 

)等.

这是代码:

 DataContractJsonSerializer ser =  DataContractJsonSerializer( typeof (Response_GetHistory));
Response_GetHistory gh =  Response_GetHistory();
gh =(Response_GetHistory)ser.ReadObject(stream); 


这是课程

 公共  class  Response_GetHistory
{
 public   int  s;
公共 Message []响应;
}
 
公共 消息
{
 
 public  字符串中;
公共 字符串 uid;
公共 字符串日期;
公共 字符串 read_state;
公共 字符串标题;
公共 字符串正文;
公共附件附件;
公共消息fwd_messages;
公共 字符串 chat_id;
公共 字符串 chat_active;
公共 字符串 users_count;
公共 字符串 admin_id;
公共 字符串已删除;
} 


但是反序列化失败;
我认为应该是259号..我应该在班级中更改什么?

解决方案

我认为您需要使用[DataContract()]属性和属性来修饰类带有[DataMember(Name =< fieldname_in_json_string>")]属性的属性,以告诉DataContractSerialiser该类的哪些属性映射到JSON中的哪个字段.例如

 [DataContract()]
公共 消息
{
[DataMember(Name = " )]
公共 字符串中{{span class ="code-keyword"> get ; 设置}
// 对所有其他对象也是如此. 



这些属性位于System.Runtime.Serialization命名空间中.希望这对您有所帮助. [ ^ ]或Json.NET [ ^ ] )-看看结果是否相同?在我看来,259值对应于 public int s; 字段,但是没有字段名称和错误的位置.您可以将"s"更改为更长的时间,只是为了看看...
Googleing透露,DataContractJsonSerialize有几个错误...


您的json似乎无效,请在此处检查: http://jsonlint.com/ [^ ]


Please , help. I am trying to deserialize json string(

{"response":[259,{"body":"я тебе ","mid":17759,"uid":18537276,"from_id":18537276,"date":1345466897,"read_state":1,"out":1},{"body":"jdjdjdjd","mid":17736,"uid":18537276,"from_id":18537276,"date":1345400400,"read_state":1,"out":1,"attachment":{"type":"doc","doc":{"did":118620269,"owner_id":18537276,"title":"IMG_20120722_220434.jpg","size":475878,"ext":"jpg","url":"http:\/\/vk.com\/doc18537276_118620269?hash=59f1fe51a02a65519c&dl=3972a9297f3da6a20c","thumb":"http:\/\/cs4343.vkontakte.ru\/u18537276\/-3\/m_69a4a578da.jpg","access_key":"7a6cb6559b179dec16"}}

) etc.

Here is code:

DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Response_GetHistory));
Response_GetHistory gh = new Response_GetHistory();
gh = (Response_GetHistory)ser.ReadObject(stream);


here is class

public class Response_GetHistory
{
public int s;
public Message[] response;
}
 
public class Message
{
 
public string mid;
public string uid;
public string date;
public string read_state;
public string title;
public string body;
public Attachments attachments;
public Message fwd_messages;
public string chat_id;
public string chat_active;
public string users_count;
public string admin_id;
public string deleted;
}


but deserialization failed;
I think becase of number 259..What should I change in my classes?

解决方案

I think you need to decorate the class with [DataContract()] attribute and properties with [DataMember(Name="<fieldname_in_json_string>")] attribute to tell the DataContractSerialiser which properties of the class to map to which field in JSON. For e.g.

[DataContract()]
public class Message
{
[DataMember(Name="mid")]
public string mid {get;set}
//so on for the all the others as well.



These attributes are in System.Runtime.Serialization namespace. Hope this helps.


Have you tried with other serializers (like fastJSON[^] or Json.NET[^])- just to see, if the result is the same? To me it looks like the 259 value is corresponding to the public int s; field, but without field name, and wrong position. Thy to change "s" to something longer, just to see...
Googleing revealed, that DataContractJsonSerialize has several bugs...


Your json seems to be invalid check it here : http://jsonlint.com/[^]


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

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