使用newtonsoft.json反序列化对象,而不使用类



我有这个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"
            ]
          }
        }
      }
    ]
  }
}

我会按字段名读取数组"items",而不使用类进行反序列化,到目前为止我所做的是:

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

示例http://www.newtonsoft.com/json/help/html/SerializingJSONFragments.htm由于我的json总是在更改,所以它使用了一个类来实现这一点,它想要这样的东西:

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

谢谢!

您离这里不远了。您想要的数据位于Ret对象内的下一级。这样试试:

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

相关内容

  • 没有找到相关文章

最新更新