0

I have a JSON file I need to catch a particular key-value to do that here my JSON code

{
    "uri": "https://wmg.nfa.futures.org/news/EnforceRegActions5imple.aspx",

      "children": [
    {
        "uri": "https://www.nfa.futures.ortnews/EnforceRegActions5imple.aspx#145View%209%28e1233a13%401",
        "children": [
            {
                "uri": "httpsghomm nfa futures org/news/EnforceReeActions5imple aspx#114Rule4%28e6ea03f2%400%40145View%209%28e1233a13%401",
                "title": "Compliance Rules : Rule 2-2 : Rule",
                "type": "Enforcement and Registration Actions : Rule",
                "publishedDate": "2019-08-15",
                "cachedDateTimeOffset": "0001-01-01700:00:00+00:00",

            },
            {
                "uri": "httos./Pv'm nfa futures ore/news/EnforceReioctions5imple 
          aspx#114Rule4%28e6ea03f2%401%40145view%209%28e1233a13%401",
                "title": "Compliance Rules : Rule 2-4 : Rule",
                "type": "Enforcement and Registration Actions : Rule",
                "publishedDate": "2019-08-15",
                "cachedDateTimeOffset": "0001-01-01T00:00:00+00:00",

            }
        ]
    "children": [
        {
            "uri": "https://www.nfa.futures.ortnews/EnforceRegActions5imple.aspx#145View%209%28e1233a13%401",
            "children": [
                {
                    "uri": "httpsghomm nfa futures org/news/EnforceReeActions5imple aspx#114Rule4%28e6ea03f2%400%40145View%209%28e1233a13%401",
                    "title": "Compliance Rules : Rule 2-2 : Rule",
                    "type": "Enforcement and Registration Actions : Rule",
                    "publishedDate": "2019-08-15",
                    "cachedDateTimeOffset": "0001-01-01700:00:00+00:00",
                    "html": "<span data-rule-id=\"RULE 2-2\" data-rule-section=\"4\" data-rule-sub-section=\"0\" class.\"btn-add-rule\" data-cube-level=\"2\" data-cube-scope=\"7404961\">Rule</span>"
                },
                {
                    "uri": "httos./Pv'm nfa futures ore/news/EnforceReioctions5imple aspx#114Rule4%28e6ea03f2%401%40145view%209%28e1233a13%401",
                    "title": "Compliance Rules : Rule 2-4 : Rule",
                    "type": "Enforcement and Registration Actions : Rule",
                    "publishedDate": "2019-08-15",
                    "cachedDateTimeOffset": "0001-01-01T00:00:00+00:00",
                    "html": "<span data-rule-id=\"RuLE 2-4\" data-rule-section=\"4\" data-rule-sub-section=\"0\" class=\"btn-add-rule\" data-cube-level=\"2\" data-cube-scope=\"64787825\">Rule</span>"
                }
            ]
        }
    ]
}

As you can see in the Image I need that Html part to catch Using C# Code I was able to get this code as JSON for an object but I cannot select this Html value to a variable

  using (StreamReader r = new StreamReader("US--NFA--INS--ENFORCE.structure.json"))
        {
            string json = r.ReadToEnd();
            var parsed = JObject.Parse(json);

when I debug I can see all the values but I can not select a particular one I also tried SElECTED Token but with no luck.

my problem is this has two children json one has an HTML key other does not have i need to get the first children I need second children that HTML value that what I need but when I nested looping it always selected the first node how to solve this

ish1104
  • 401
  • 4
  • 19

3 Answers3

1

I can't see the image but you can get by:

parsed["key"];
Ömer Baş
  • 98
  • 1
  • 7
  • there are several levels of that Jason as example children have several levels the last level has the value that I need – ish1104 Aug 22 '19 at 11:48
  • 1
    Use a VPN service like Opera's embedded VPN. Imgur is banned in Turkey. – Celal Ergün Aug 22 '19 at 11:48
  • @Ömer Baş it has several levels so I cannot take directly using key I need to go to the last node level of children to get the value – ish1104 Aug 22 '19 at 11:50
  • 1
    You can get deeper like parsed["key1"]["key2"]. But if there are several levels, I suggest to create a class then map your json to it. Thank you @CelalErgün. – Ömer Baş Aug 22 '19 at 11:56
1

Something along the lines of below should do the trick.

JArray data = (JArray)parsed["children"];

foreach(JObject children in (JObject)data){
  foreach(JObject child in (JArray)children["children"]){
     var value = child["html"];
  }

}
Adrian
  • 8,271
  • 2
  • 26
  • 43
  • it gives a null Value Child object get all the values I need but it also returns a null value for this var value. Also here I have more than one string for the Html it should store as a list. – ish1104 Aug 22 '19 at 12:00
1

Maybe this example will help:

string json = @"{
  'channel': {
    'title': 'James Newton-King',
    'link': 'http://james.newtonking.com',
    'description': 'James Newton-King\'s blog.',
    'item': [
      {
        'title': 'Json.NET 1.3 + New license + Now on CodePlex',
        'description': 'Announcing the release of Json.NET 1.3, the MIT license and the source on CodePlex',
        'link': 'http://james.newtonking.com/projects/json-net.aspx',
        'categories': [
          'Json.NET',
          'CodePlex'
        ]
      },
      {
        'title': 'LINQ to JSON beta',
        'description': 'Announcing LINQ to JSON',
        'link': 'http://james.newtonking.com/projects/json-net.aspx',
        'categories': [
          'Json.NET',
          'LINQ'
        ]
      }
    ]
  }
}";

JObject rss = JObject.Parse(json);

string rssTitle = (string)rss["channel"]["title"];

string itemTitle = (string)rss["channel"]["item"][0]["title"];

JArray categories = (JArray)rss["channel"]["item"][0]["categories"];
  • this is hard coding is there any way to do this by without hardcoding – ish1104 Aug 22 '19 at 12:13
  • I can have two children node frist children node does not contain an HTML key second one has I need to get the second node call to a function.usi.ng Adriani6 answer i able to get the value but it calls the first children data but I need second children data – ish1104 Aug 22 '19 at 12:43