2012-09-18 79 views
3

我有一个小的解决方案,我做了一个关于这个主题的研究,但是找不到我正在寻找的东西,例子是编译一个字符串或完整表达式的整个方法。我想是说我有这样的代码,我想从使用Newtonsoft.json一个JSON中提取数据,将字符串转换为c#可执行代码

 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": "" } }]} 

任何想法如何或者是它甚至可能吗?

+0

这是可能的。你可以用System.CodeDom.Compiler编译代码(我希望ns是正确的) – TGlatzer

+0

你在寻找一个Eval函数吗?有一个关于它的相关问题:http://stackoverflow.com/questions/6052640/in-c-sharp-is-there-an-eval-function – Larry

+0

嘿谢谢你们两位的回复,我会检查这些:) –

回答

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 

这是你需要什么?

+0

嘿,我更新了与我使用JSON的问题,我怎么能将你的代码应用到我提供的JSON字符串?..我认为这正是我正在寻找的,因为我想查看Property.name值,但我无法确认,因为代码是在我的研究所:)非常感谢你的帮助:) –

+0

嗯,嘿,我试过了,但这又是静态的,它适用于你的json格式,但不mine.i想要的东西这可以在任何JSON运行时工作:)再次感谢您的帮助..任何想法如何执行此? –

+1

你是什么意思静态? – Larry