-2

我在 c# 中有一个 JSON 对象。我想在 c# 中迭代​​它。对象中的所有属性都是动态的。在下面的代码中tr5、tr5td7、tr6、tr6td4是动态名称,可以在另一个对象中更改。所以请帮助我。

  "trList": {
"tr5": {
  "tdList": {
    "tr5td7": {
      "td": {
        "outcomesName": "Weight (kg) Simvastatin",
        "group": {
          "groups": "Entry",
          "dosage": "",
          "n": ""
        },
        "values": [
          {
            "type": "Mean",
            "symbolType": "x¯",
            "value": "78.4"
          },
          {
            "type": "SE",
            "symbolType": "S",
            "value": "3.1"
          }
        ],
        "pattern": {
          "arr": [
            {
              "values": "78.4",
              "pattern": "number",
              "type": "Mean",
              "symbolType": "x¯"
            },
            {
              "values": "3.1",
              "pattern": "number",
              "type": "SE",
              "symbolType": "S"
            }
          ],
          "pat": "number-separator-number-separator-",
          "tdId": "tr5td7"
        },
        "note": "",
        "outcomesNote": "",
        "groupsNote": ""
      }
    },
  }
 }
     "tr6": {
  "tdList": {
    "tr6td4": {
      "td": {
        "outcomesName": "Height (kg) Simvastatin",
        "group": {
          "groups": "Entry",
          "dosage": "",
          "n": ""
        },
        "values": [
          {
            "type": "Mean",
            "symbolType": "x",
            "value": "70.4"
          },
          {
            "type": "SE",
            "symbolType": "S",
            "value": "5"
          }
        ],
        "pattern": {
          "arr": [
            {
              "values": "70.4",
              "pattern": "number",
              "type": "Mean",
              "symbolType": "x"
            },
            {
              "values": "5",
              "pattern": "number",
              "type": "SE",
              "symbolType": "S"
            }
          ],
          "pat": "number-separator-number-separator-",
          "tdId": "tr6td4"
        },
        "note": "",
        "outcomesNote": "",
        "groupsNote": ""
      }
    },
  }
 }

}

4

2 回答 2

0

您可以使用 JSON.Net。

dynamic d = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");

Console.WriteLine(d.number);
Console.WriteLine(d.str);
Console.WriteLine(d.array.Count);
于 2013-06-19T07:53:50.853 回答
0

您的dynamic对象实际上只是 a Dictionary<string, object>,因此您可以将其转换为例如

IDictionary<string, object> dict = jsonObject;
foreach (var item in dict)
{
    Console.WriteLine("{0}: {1}", item.Key, item.Value);
}
于 2013-06-19T07:43:33.693 回答