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

8
  • 4
    simply create the model and then deserialize it and then access value. Commented Aug 22, 2019 at 11:43
  • here everything you need to do with json in .net newtonsoft.com/json Commented Aug 22, 2019 at 12:01
  • 1
    Do you need all the html values or just one? Commented Aug 22, 2019 at 12:08
  • @TedBrownlow all HTML Values Commented Aug 22, 2019 at 12:12
  • Possible duplicate of JSON.NET deserialize a specific property Commented Aug 22, 2019 at 12:17

3 Answers 3

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

}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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

parsed["key"];

4 Comments

there are several levels of that Jason as example children have several levels the last level has the value that I need
Use a VPN service like Opera's embedded VPN. Imgur is banned in Turkey.
@Ö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
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.
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"];

2 Comments

this is hard coding is there any way to do this by without hardcoding
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.