使用JSON.NET或JavaScriptSerializer在C#中反序列化JSON对象

时间:2011-05-01 17:41:55

标签: .net json

很抱歉,如果之前有人问这个问题,但我试着在这里寻找答案,但我找不到答案。

无论如何,我对这个问题有类似的问题: Parsing JSON using Json.net

它解决了一些问题,但我对我的JSON数据感到困惑。这是json数据

{
   "messages":[
      {
         "message":"hilo",
         "timestamp":"23:55",
         "uid1":"7",
         "name":"shiftypowers"
      }
   ],
   "total_messages":"1",
   "last_timestamp":"1304265309",
   "buddylist":{
      "476":{
         "name":"Gandang_hari",
         "status":"1"
      },
      "506":{
         "name":"ichigo",
         "status":"1"
      },
      "186":{
         "name":"Jinn",
         "status":"1"
      },
      "7":{
         "name":"shiftypowers",
         "status":"1"
      },
      "total":4
   }
}

如果您注意到数字476,506,186和7 - 这些是用户ID,我将来会处理数千个用户ID。

问题是,我该如何反序化呢?

这是我的代码

public class ElakoChatPollData
    {
        public ElakoChatPollData()
        {
            messages = new List();
        }
        public List messages { get; set; }
        public string total_messages { get; set; }
        public string last_timestamp { get; set; }
        public buddylist buddylist { get; set; }
    }

    public class messages
    {
        public string message { get; set; }
        public string timestamp { get; set; }
        public string uid1 { get; set; }
        public string name { get; set; }
    }

    public class buddylist
    {
        // what should I put here ??
        //public List uid { get; set; } // i don't think this is correct
        public Dictionary Users { get; private set; }
        public string total { get; set; }
    }

    public class userinfo
    {
        public string name { get; set; }
        public string status { get; set; }
    }

// and serializing the json looks like this
JavaScriptSerializer ser = new JavaScriptSerializer();
ChatPollData epd = ser.Deserialize<ChatPollData>(jsonchatpoll);
Console.Writeline(epd.buddylist.Users[0].name); // i get Object reference not set ~~~~

如果上面的代码是正确的..我怎么能得到用户ID?顺便说一句,json数据将从drupal模块(drupalchat)请求

2 个答案:

答案 0 :(得分:0)

一个简单的解决方案是使用newtonsoft json库中包含的linq-to-json类。然后,您需要手动实例化您的对象,但是给出了非常简单的文件格式,这应该不会有多大用处。

手动执行它的好处是数据格式和内存格式可能完全不同。

 // public List<userinfo> uid { get; set; }; // i don't think this is correct
 public Dictionary<int,UserInfo> Users{get;private set;}//I might use something like this

答案 1 :(得分:0)

我认为您可以将Newtonsoft.Json库用于序列化或反序列化过程。

Id值必须在BuddyList对象中(这是我的看法)

如果您的回复是这样的:

{
    "messages": [{
        "message": "hilo",
        "timestamp": "23:55",
        "uid1": "7",
        "name": "shiftypowers"
    }],
    "total_messages": "1",
    "last_timestamp": "1304265309",
    "buddylist": [{
            "id": 476,
            "name": "Gandang_hari",
            "status": "1"
        }, {
            "id": 506,
            "name": "ichigo",
            "status": "1"
        }, {
            "id": 186,
            "name": "Jinn",
            "status": "1"
        },
        {
            "id": 7,
            "name": "shiftypowers",
            "status": "1"
        }
    ]
}

您可以创建反序列化响应对象:

public class Message
{
    public string message { get; set; }
    public string timestamp { get; set; }
    public string uid1 { get; set; }
    public string name { get; set; }
}

public class Buddylist
{
    public int id { get; set; }
    public string name { get; set; }
    public string status { get; set; }
}

public class ElakoChatPollData
{
    public List<Message> messages { get; set; }
    public string total_messages { get; set; }
    public string last_timestamp { get; set; }
    public List<Buddylist> buddylist { get; set; }
}

要序列化该对象时:

string _serialized = JsonConvert.SerializeObject(_yourObject);

对于反序列化:

var _data = JsonConvert.DeserializeObject<ElakoChatPollData>(_serialized);