1
votes

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)

2
Are you able to modify the above JSON to include a uid like this? "uid" : "476", "userinfo" : { "name":"Gandang_hari", "status":"1" }Nicky Waites
nope. the above json data will be requested via url from drupalchat modulejaysonragasa
Are you posting this to a web service? Maybe take a look at how to deserialize a dictionary weblogs.asp.net/hajan/archive/2010/07/23/…Nicky Waites

2 Answers

0
votes

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
0
votes

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