3

I have a small solution, I did a research on this topic but couldn't find exactly what i am looking for, the examples was either to compile a whole method in a string or a full expression. What i want is say i have this code which i am trying to extract data from a json using Newtonsoft.json,

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

I want is to pass this section,

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

as string and convert to c# code so that i can make that value dynamic as json files format very from one another.

EDIT:

following is the json string i am working with,

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

any idea how to or is it even possible??

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Hasitha Shan
  • 2,900
  • 6
  • 42
  • 83

1 Answers1

2

Could you show, how your JSON Data looks like? By Example:

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

Now you could iterate through the elements:

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);
            }
        }
    }
}

Gives this Output:

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

Is this what you needed?

eMi
  • 5,540
  • 10
  • 60
  • 109
  • hey, i updated the question with the json i use, how can i apply your code to the json string i provided?..I think this is just what i am looking for because i want to view the Property.name values but i can't confirm because the code is at my institute :) thank you very much for the help :) – Hasitha Shan Sep 18 '12 at 15:04
  • ummm hey, i tried this out but this is again static, It works for your format of json but not mine.i want something that can work for any json at run time :) thanks again for your help..any idea how to perfrom this?? – Hasitha Shan Sep 19 '12 at 03:32
  • ohh...please forgive for the late reply, static as in the format of jsons can vary, as in where the actual data is located that i need to process. so i need something that an be applied for any json format :) – Hasitha Shan Oct 04 '12 at 04:01
  • you can see from the json you showd and the json i have posted :) – Hasitha Shan Oct 04 '12 at 04:02