与反序列化一个的JavaScriptSerializer

与反序列化一个的JavaScriptSerializer

本文介绍了与反序列化一个的JavaScriptSerializer JSON文件()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JSON文件的结构,我会反序列化看起来像下面;

  {
    ID:1lad07
    文:测试,
    URL:HTTP:\\ / \\ / twitpic.com \\ / 1lacuz
    宽:220,
    高度:84,
    大小:8722,
    类型:PNG
    时间戳:2010年16时十一分48秒+0000星期三,5月5日
    用户:{
        ID:12345,
        SCREEN_NAME:twitpicuser
    }
}

我创建了具有申请名称作为的JavaScriptSerializer属性的类。我将用反序列化JSON的$​​ C $ c是如下:

 使用(VAR读者=新的StreamReader(twitpicResponse.GetResponseStream())){
                变种responseBody = reader.ReadToEnd();
                VAR解串器=新的JavaScriptSerializer();
                VAR的结果= deserializer.Deserialize<应变及GT;(responseBody);            }

我的问题是我怎么能读取JSON文件的用户字段。这是像下面;

 用户:{
    ID:12345,
    SCREEN_NAME:twitpicuser
}

有子属性和值。我怎么可以说出他们对我的反应类。我的回应类现在这个样子;

 公共类响应{    公共字符串ID {搞定;组; }
    公共字符串文本{搞定;组; }
    公共字符串URL {搞定;组; }
    公共字符串宽度{搞定;组; }
    公共字符串高度{搞定;组; }
    公共字符串大小{搞定;组; }
    公共字符串类型{搞定;组; }
    公共字符串时间戳{搞定;组; }}

什么是做的最好的情况?


解决方案

  1. 您需要创建一个保存用户价值的一类,就像响应类用户

  2. 属性添加到与用户价值的新类的类型响应类用户用户

     公共类响应{    公共字符串ID {搞定;组; }
        公共字符串文本{搞定;组; }
        公共字符串URL {搞定;组; }
        公共字符串宽度{搞定;组; }
        公共字符串高度{搞定;组; }
        公共字符串大小{搞定;组; }
        公共字符串类型{搞定;组; }
        公共字符串时间戳{搞定;组; }
        公共用户用户{搞定;组; }}公共类用户{    公众诠释ID {搞定;组; }
        公共字符串SCREEN_NAME {搞定;组; }}


一般来说,你应该确保物业类型JSON和您的CLR类相匹配。看来,你试图反序列化结构包含多个数值(最有可能的 INT )。我不知道如果的JavaScriptSerializer 能够自动数字将反序列化的字符串字段。但是,你应该尽量满足你的CLR类型尽可能接近实际的数据尽可能反正。

the json file's structure which I will deserialize looks like below;

{
    "id" : "1lad07",
    "text" : "test",
    "url" : "http:\/\/twitpic.com\/1lacuz",
    "width" : 220,
    "height" : 84,
    "size" : 8722,
    "type" : "png",
    "timestamp" : "Wed, 05 May 2010 16:11:48 +0000",
    "user" : {
        "id" : 12345,
        "screen_name" : "twitpicuser"
    }
}

I have created a class which has the filed names as properties for JavaScriptSerializer. The code which I will use to Deserialize the json is as follows;

            using (var reader = new StreamReader(twitpicResponse.GetResponseStream())) {


                var responseBody = reader.ReadToEnd();
                var deserializer = new JavaScriptSerializer();
                var results = deserializer.Deserialize<Response>(responseBody);

            }

My problem is how I can read the user field on json file. which is like below;

"user" : {
    "id" : 12345,
    "screen_name" : "twitpicuser"
}

it has sub properties and values. how can I name them on my Response class. my response class now look like this;

public class Response {

    public string id { get; set; }
    public string text { get; set; }
    public string url { get; set; }
    public string width { get; set; }
    public string height { get; set; }
    public string size { get; set; }
    public string type { get; set; }
    public string timestamp { get; set; }

}

what is the best case to do it?

解决方案
  1. You need to create a class that holds the user values, just like the response class User.
  2. Add a property to the Response class 'user' with the type of the new class for the user values User.

    public class Response {
    
        public string id { get; set; }
        public string text { get; set; }
        public string url { get; set; }
        public string width { get; set; }
        public string height { get; set; }
        public string size { get; set; }
        public string type { get; set; }
        public string timestamp { get; set; }
        public User user { get; set; }
    
    }
    
    public class User {
    
        public int id { get; set; }
        public string screen_name { get; set; }
    
    }
    

In general you should make sure the property types of the json and your CLR classes match up. It seems that the structure that you're trying to deserialize contains multiple number values (most likely int). I'm not sure if the JavaScriptSerializer is able to deserialize numbers into string fields automatically. But you should try to match you're CLR type as close to the actual data as possible anyway.

这篇关于与反序列化一个的JavaScriptSerializer JSON文件()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:03