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

发布于 2024-11-04 06:02:10 字数 2247 浏览 0 评论 0原文

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

无论如何,我对这个问题也有类似的问题: 使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空城之時有危險 2024-11-11 06:02:10

一种简单的解决方案是使用 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

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.

 // 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
蔚蓝源自深海 2024-11-11 06:02:10

我认为您可以使用 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);

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:

{
    "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"
        }
    ]
}

You can create response object for deserialize:

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; }
}

When you want to serialize this object:

string _serialized = JsonConvert.SerializeObject(_yourObject);

For Deserialize:

var _data = JsonConvert.DeserializeObject<ElakoChatPollData>(_serialized);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文