how to parse json array using Litjson?

时光总嘲笑我的痴心妄想 提交于 2019-11-29 14:55:02

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<Service> services { get; set; }
}

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

UserServices services = JsonMapper.ToObject<UserServices>(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"]);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!