使用 JSON.NET 或 JavaScriptSerializer 在 C# 中反序列化 JSON 对象
很抱歉,如果之前有人问过这个问题,但我尝试在这里寻找一些答案,但找不到。
无论如何,我对这个问题也有类似的问题: 使用 Json.net 解析 JSON
它解决了一些问题,但对我的 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 } }
If you noticed the numbers 476, 506, 186, and 7 - those were user id, and am going to deal with thousands of user id in the future.
The question is, how do I deserialize this?
如果上面的代码正确的话,这是我的代码
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) 请求
am sorry if this was asked before but I tried looking for some answers here but I coudln't find one.
Anyway, I have similar problem with this question:
Parsing JSON using Json.net
it solves some problem but am confused about my JSON data. here's the json data
{ "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 } }
If you noticed the numbers 476, 506, 186, and 7 - those were user id, and am going to deal with thousands of user id in the future.
The question is, how do I deserialize this?
here's my code
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 ~~~~
if the code above is correct.. how may I able to get the user ids? and btw, the json data will be requested from drupal module (drupalchat)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种简单的解决方案是使用 newtonsoft json 库中包含的 linq-to-json 类。然后,您需要手动实例化您的对象,但考虑到您的文件格式非常简单,这应该不需要太多工作。
手动执行此操作的好处是数据格式和内存格式可能完全不同。
One simple solution is using the linq-to-json classes included with the newtonsoft json library. You then need to manually instantiate your objects, but given your very simple file format that shouldn't be much work.
The nice thing about doing it manually is that the data-format and your in memory format can be quite different.
我认为您可以使用 Newtonsoft.Json 库进行序列化或反序列化过程。
Id 值必须在 BuddyList 对象中(这是我的意见)
如果您的响应如下:
您可以为反序列化创建响应对象:
当您想要序列化此对象时:
对于反序列化:
I think you can use Newtonsoft.Json library for Serialize or Deserialize process.
Id value must be in BuddyList object(this is my opinion)
If your response like this:
You can create response object for deserialize:
When you want to serialize this object:
For Deserialize: