3

我有一个小解决方案,我对此主题进行了研究,但找不到我正在寻找的确切内容,示例是在字符串中编译整个方法或完整表达式。我想要说我有这段代码,我试图使用 Newtonsoft.json 从 json 中提取数据,

        JObject o = JObject.Parse(data);
        string getFristRow = Convert.ToString(o["Body"][0]["RowId"]);

我想要的是通过这个部分,

        o["Body"][0]["RowId"]

作为字符串并转换为 c# 代码,这样我就可以使该值动态地作为 json 文件格式彼此非常不同。

编辑:

以下是我正在使用的 json 字符串,

       { "Head": { "Status": 0, "Message": "", "Count": 1905 }, "Body": [ { "RowId": { "SensorIdValue": "Sensor-029-cert.org.cn", "DataTimeValue": "20120911100002", "DataInValue": "eth0", "DataOutValue": "", "DataMacSourceValue": "3c:e5:a6:55:2b:1a", "DataMacDestinationValue": "00:0c:29:80:1d:fc", "DataMacTypeValue": "08:00", "DataUidValue": "0", "ProtocolValue": "Tcp", "IpPrecedenceValue": "0x00", "IpTypeOfServiceValue": "0x00", "IpTotalLengthValue": "48", "IpIdentificationValue": "35856", "IpFragmentOffsetValue": "0", "IpMoreFragmentValue": "0", "IpTruncatedValue": "0", "IpCongestionExperiencedValue": "0", "IpTimeToLiveValue": "116", "IpFragValue": "0", "IpOptionValue": "", "IpSourceAddressValue": "61.157.198.130", "IpDestinationAddressValue": "202.108.212.84", "SourceRegionValue": "CN", "DestinationRegionValue": "CN", "SourcePortValue": "1729", "DestinationPortValue": "5900", "SequenceNumberValue": "0", "AcknowledgmentNumberValue": "0", "WindowValue": "65535", "ReservedValue": "0x00", "UrgentPointerValue": "0 ", "CrwValue": "0", "EceValue": "0", "UrgValue": "0", "AckValue": "0", "PshValue": "0", "RstValue": "0", "SynValue": "1", "FinValue": "0", "TruValue": "0", "OptionsValue": " ", "LengthValue": "", "TypeValue": "", "CodeValue": "", "IdentificationValue": "", "ParameterValue": "", "GatewayValue": "", "MaximumTransmissionUnitValue": "", "IncompleteValue": "", "SpiValue": "", "InfoValue": "" } }, { "RowId": { "SensorIdValue": "Sensor-029-cert.org.cn", "DataTimeValue": "20120911100003", "DataInValue": "eth0", "DataOutValue": "", "DataMacSourceValue": "3c:e5:a6:55:2b:1a", "DataMacDestinationValue": "00:0c:29:80:1d:fc", "DataMacTypeValue": "08:00", "DataUidValue": "0", "ProtocolValue": "Tcp", "IpPrecedenceValue": "0x00", "IpTypeOfServiceValue": "0x00", "IpTotalLengthValue": "44", "IpIdentificationValue": "13483", "IpFragmentOffsetValue": "1", "IpMoreFragmentValue": "0", "IpTruncatedValue": "0", "IpCongestionExperiencedValue": "0", "IpTimeToLiveValue": "116", "IpFragValue": "0", "IpOptionValue": "", "IpSourceAddressValue": "183.61.185.3", "IpDestinationAddressValue": "202.108.212.84", "SourceRegionValue": "CN", "DestinationRegionValue": "CN", "SourcePortValue": "80", "DestinationPortValue": "41084", "SequenceNumberValue": "0", "AcknowledgmentNumberValue": "0", "WindowValue": "8760", "ReservedValue": "0x00", "UrgentPointerValue": "0 ", "CrwValue": "0", "EceValue": "0", "UrgValue": "0", "AckValue": "1", "PshValue": "0", "RstValue": "0", "SynValue": "1", "FinValue": "0", "TruValue": "0", "OptionsValue": " ", "LengthValue": "", "TypeValue": "", "CodeValue": "", "IdentificationValue": "", "ParameterValue": "", "GatewayValue": "", "MaximumTransmissionUnitValue": "", "IncompleteValue": "", "SpiValue": "", "InfoValue": "" } }]}

知道如何或什至可能吗?

4

1 回答 1

2

您能否展示一下您的 JSON 数据是什么样的?举例:

{
  "1": {
    "id"  : 1,
    "name": Eni,
    "type": "Girl"
  },
  "2": {
    "id"  : 2,
    "name": Maarten,
    "type": "Men"
  }
}

现在您可以遍历元素:

var jFoo = JObject.Parse(data);
foreach (JToken child in jFoo.Children())
{
    foreach (JToken grandChild in child)
    {
        foreach (JToken grandGrandChild in grandChild)
        {
            var property = grandGrandChild as JProperty;
            if (property != null)
            {
                Console.WriteLine(property.Name + ":" + property.Value);
            }
        }
    }
}

给出这个输出:

id:1
name:Eni
type:Girl
id:2
name:Maarten
type:Men

这是你需要的吗?

于 2012-09-18T11:27:37.470 回答