c# - 将字符串转换为 C# 可执行代码

标签 c# asp.net json.net dynamic-compilation dynamic-code

我有一个小的解决方案,我对这个主题进行了研究,但找不到我正在寻找的确切内容,这些示例是将整个方法编译为字符串或完整表达式。我想要的是说我有这段代码,我正在尝试使用 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": "" } }]}

知道怎么做或者甚至可能吗?

最佳答案

您能展示一下您的 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

这是您需要的吗?

关于c# - 将字符串转换为 C# 可执行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12475804/

相关文章:

C# 使用反射访问窗口属性

c# - 用于列表项的 Asp.net razor 文本框数组

c# - ASP.Net C# - SqlDataSource 和 Oracle : The network path was not found

c# - 如何使用 Json.Net 序列化/反序列化具有附加属性的自定义集合

C# 语音识别

c# - 如果解析不好,则解析 JSON 语法

WebForms 404 中的 ASP.NET Web API

c# - 无法使用阅读器从 Excel 中读取特定单元格

c# - Newtonsoft 在序列化时对子元素进行内联格式化

c# - 使用 Newtonsoft 解析嵌入式多维十进制数组