0

I have this Joson

{
  "Sucess": true,
  "Msg": "OK",
  "Ret": {
    "First": 0,
    "Next": true,
    "Total": 60,
    "Itens": [
      {
        "ID": 212121,
        "Name": "uuuuuuuuuuuuuuuuuuuuuuuu",
        "LcID": 9898,
        "Oclao": false,
        "Lal": {
          "ID": 12202,
          "Name": "pppppppppppppppppp",
          "Pais": "Brasil",
          "Dtc": 0.0
        },
        "Subtipo": {
          "ID": 7458,
          "Desc": "mnmnmnmnn"
        },
        "Tipo": {
          "Sit": "cor1",
          "Sitrm": 0,
          "Name": "Shsdfow"
        },
        "Qtde": 0,
        "Qntcoes": 0,
        "Pubum": "adfsdfsdfs",
        "Evias": {
          "arq": {
            "Mo": [
              "site.com"
            ],
            "Moir": [
              "site.com"
            ]
          }
        }
      },
      {
        "ID": 9797878,
        "Name": "uuuuuuuuuuuuuuuuuuuuuuuu",
        "LcID": 9898,
        "Oclao": false,
        "Lal": {
          "ID": 12332,
          "Name": "pppppppppppppppppp",
          "Pais": "Brasil",
          "Dtc": 0.0
        },
        "Subtipo": {
          "ID": 7458,
          "Desc": "mnmnmnmnn"
        },
        "Tipo": {
          "Sit": "cor1",
          "Sitrm": 0,
          "Name": "Shsdfow"
        },
        "Qtde": 0,
        "Qntcoes": 0,
        "Pubum": "adfsdfsdfs",
        "Evias": {
          "arq": {
            "Mo": [
              "site.com"
            ],
            "Moir": [
              "site.com"
            ]
          }
        }
      }
    ]
  }
}

I would read the array "items" by the field names without using a class to Deserialize, what I did so far was:

JObject jRetorno = JObject.Parse(strJson);
IList<JToken> jItens = jRetorno["Itens"].Children().ToList();

The example http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm that uses a class for this, as my json always changes, wanted something like:

strReturn = jItens[1]["ID"];
strReturn = jItens[1]["Name"];
strReturn = jItens[2]["ID"];
strReturn = jItens[2]["Name"];
strReturn = jItens[3]["ID"];
strReturn = jItens[3]["Name"];

Thanks!

1
  • Why don't you want to deserialize the whole thing? Commented Nov 17, 2015 at 20:58

1 Answer 1

1

You're not that far off. The data you want is one level further down, inside the Ret object. Try it like this:

JObject jRetorno = JObject.Parse(strJson);
IList<JToken> jItens = jRetorno["Ret"]["Itens"].Children().ToList();
foreach (JToken jt in jItens)
{
    Console.WriteLine(jt["ID"]);
    Console.WriteLine(jt["Name"]);
}

Fiddle: https://dotnetfiddle.net/TwtGyz

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.