简体   繁体   中英

C# Parsing JSON Object

I am wondering if we can take some parts inside a JSON string and print it out. I have a JSON string and I only want to take some specific things in it. My JSON string:

{
"A": {
   "a1":[
      {
         "a11": {
              "a111": "something1",
              "a112": "something2"
          },
         "a12": [
             {
                "a121": "something1",
                "a122": "something2"
             },
             {
                "a211": "something1",
                "a212": "soemthing2"
             }
           ]
       ]
  },
 "B": {
   "b1":[
      {
         "b11": {
              "b111": "something1",
              "b112": "something2"
          },
         "b12": [
             {
                "b121": "something1",
                "b122": "something2"
             },
             {
                "b211": "something1",
                "b212": "soemthing2"
             }
           ]
       }
    ]
   }
 }

I only want to print out: b121 is something1 . How can I do it in C#?

Firstly, your JSON example has some problems so I'll give an example using this part as the JSON file.

{
"B": {
   "b1":
      {
         "b11": {
              "b111": "something1",
              "b112": "something2"
          },
         "b12": [
             {
                "b121": "something1",
                "b122": "something2"
             },
             {
                "b211": "something1",
                "b212": "soemthing2"
             }
           ]
       }
  }
}

You can access to a specific part like this using Json.NET :

var jsonData = File.ReadAllText(@"\test.json");

dynamic jsonObject = JsonConvert.DeserializeObject(jsonData);

// print out a specific part
Console.WriteLine(jsonObject.B.b1.b11.b111);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM