2012-09-18 78 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