Parsing a complex JSON result with C#

夙愿已清 提交于 2019-12-01 21:02:34
Manvik

Use the below classes for de-serializing using JSON.Net

public class ResponseActual
{

    [JsonProperty("response")]
    public Response2 Response { get; set; }
}

public class Response2
{

    [JsonProperty("result")]
    public Result Result { get; set; }

    [JsonProperty("uri")]
    public string Uri { get; set; }
}

public class Result
{

    [JsonProperty("Contacts")]
    public Contacts Contacts { get; set; }
}

public class Contacts
{

    [JsonProperty("row")]
    public IList<Row> Row { get; set; }
}

  public class Row
{

    [JsonProperty("no")]
    public string No { get; set; }

    [JsonProperty("FL")]
    public IList<FL> FL { get; set; }
}

 public class FL
{

    [JsonProperty("content")]
    public string Content { get; set; }

    [JsonProperty("val")]
    public string Val { get; set; }
}

//To De-serialize
ResponseActual respone = JsonConvert.DeserializeObject<ResponseActual>(jSON_sTRING)

//Get the contacts list
List<FL> contacts = respone.Response.Result.Contacts.Row[0].FL.ToList();

//Now Get the required value using LINQ
var value = contacts.Where<FL>((s, e) => s.Val =="Email").Select(x=>x.Content).Single();

You may also checkout this - Deserializing JSON to .NET object using Newtonsoft (or LINQ to JSON maybe?)

You need to include the following portion of JSON in your deserialization object Type :

{
"response":
{
    "result":
    {
        "Contacts":
        {
            "row":
            [
                {
                    "no":"1",
                    "FL":

Class type : 'Contact' is inadequate.

you can use this code:

    dynamic dictionary = (JsonConvert.DeserializeObject>(jsonstring))["response"];


            var result = dictionary.result;
            var contact= result.Contacts;
            var row= contact.row;

            foreach (var item in row)
            {

                var no= item.no;

            }

Can I ask you something? Are you the one exporting the JSON? I ask this because the format is quite weird and it does get in the way of your code.

A simpler format would allow you to serialize the string pretty much in a direct way.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!