how to parse json array using Litjson?

后端 未结 1 1423
忘掉有多难
忘掉有多难 2020-12-21 08:33

I am developing an application in which i am using data came from server in the json format. However i am able to parse normal json data but failed to parse the json data wi

1条回答
  •  隐瞒了意图╮
    2020-12-21 09:08

    You should create yourselft following POCO objects:

    public class Service
    {
        public int idservice { get; set; }
    
        public string title { get; set; }
    
        public string message { get; set; }
    }
    
    public class UserServices
    {
        public int id_user { get; set; }
    
        public List services { get; set; }
    }
    

    LitJSON will deserialize this out-of-the-box:

    UserServices services = JsonMapper.ToObject(rawJson);
    

    As an alternative you can use non-generic variant (below sample would wrote all data to the console):

    JsonData data = JsonMapper.ToObject(rawJson);
    Console.WriteLine("User id: {0}", data["id_user"]);
    Console.WriteLine("Services:");
    for (int i = 0; i < data["services"].length; i++)
    {
        Console.WriteLine ("    Id: {0}; Title: {1}; Message {2}", data["services"][i]["idservice"], data["services"][i]["title"], data["services"][i]["message"]);
    }
    

    0 讨论(0)
提交回复
热议问题