Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi

I have JSON response and I want to get the Item and Position values. Where is it in the tree structure I want the Item and Position. In C# WPF
"address": [
    {
      "item": {
        "country": "USA",
        "locality": "Rio Rancho",
        "postal_code": "87124",
        "region": "New Mexico",
        "street": "1380 Rio Rancho Blvd SE #363",
        "type": [
          "work"
        ]
      },
      "position": "168,124,399,124,399,169,168,169"
    }
  ],
  "comment": [
    {
      "item": "CELL",
      "position": "554,277,596,277,596,291,554,291"
    }
  ],
  "email": [
    {
      "item": "aynejames@me.com",
      "position": "532,114,746,114,746,139,532,139"
    }
  ],
  "formatted_name": [
    {
      "item": "Wayne Stansfield. CLCS",
      "position": "55,178,399,178,399,208,55,208"
    }
  ],
  "label": [
    {
      "item": {
        "address": "1380 Rio Rancho Blvd SE #363 Rio Rancho, NM 87124, 87124",
        "type": [
          "work"
        ]
      },
      "position": "168,124,399,124,399,169,168,169"
    }
  ],

"name": [
    {
      "item": {
        "family_name": "CLCS",
        "given_name": "Wayne Stansfield."
      },
      "position": "0,0,0,0,0,0,0,0"
    }
  ],
  "organization": [
    {
      "item": {
        "name": "Luarris Insurance"
      },
      "position": "54,300,400,300,400,351,54,351"
    },
    {
      "item": {
        "name": "A Legacy Of Qu(zlity Service '"
      },
      "position": "124,271,393,271,393,294,124,294"
    }
  ],
If there are two Items like in Organization tab it should return both Item1,2 and Position1,2 .

It is completely dynamic Address, Name, Email which are the starting Key values of the JSON response those also be may or may not there. And if there is any new key then the Item and Position value of that new key should also be returned.

Can anyone help me out from this problem?


What I have tried:

I have tried to make it dynamic but what I get only static method to so.

Please help me to get out this problem.
Posted
Updated 17-Sep-18 5:37am

I wrote an article for questions like this one: Working with JSON in C# & VB[^] ... It will answer all of your questions for you on JSON deserialization
 
Share this answer
 
That's a big blob of JSON. I ran it through json linter (JSONLint - The JSON Validator[^]) and it isn't valid so it makes it more difficult to give you a quick answer.

So, I grabbed just the first part of it to lead you toward a solution:

JavaScript
[{
 	"item": {
 		"country": "USA",
 		"locality": "Rio Rancho",
 		"postal_code": "87124",
 		"region": "New Mexico",
 		"street": "1380 Rio Rancho Blvd SE #363",
 		"type": [
 			"work"
 		]
 	},
 	"position": "168,124,399,124,399,169,168,169"
 }]


You can figure this out for yourself if you'll do the following:
1) Open up the browser dev tools (usually F12)
2) copy the json above.
3) type the following in the dev tools command console:
JavaScript
var address =  [{
 	"item": {
 		"country": "USA",
 		"locality": "Rio Rancho",
 		"postal_code": "87124",
 		"region": "New Mexico",
 		"street": "1380 Rio Rancho Blvd SE #363",
 		"type": [
 			"work"
 		]
 	},
 	"position": "168,124,399,124,399,169,168,169"
 }]


Obviously you type
JavaScript
var address =

and then paste in the json and then press <enter>

Now you can examine the address object yourself.

In the dev console, type the following and press <enter>

JavaScript
address[0].item.country


When you do that, the console will display: USA
Here's a snapshot of what it looks like, running at the bottom of this (CP) page while I type this answer.
https://i.stack.imgur.com/ItMXd.png[^]

Now you can fix your JSON and set your object and try it out for yourself.
 
Share this answer
 
Thanks to both of you for your valuable answers.
Here is my way what I have done to fix this.

As I am interested in Item and Position of the each and every block.


string txtValue = "";
string positionValue = "";
foreach (dynamic property in json.Properties())
{
    if (property.Value.HasValues)
    {
        for (int j = 0; j < property.Value.Count; j++)
        {
            foreach (JProperty data in property.Value[j])
            {
                if (data.Name == "item")
                {
                    foreach (dynamic item in property.Value[j])
                    {
                        if (data.Name == "item" && item.Name != "position")
                        {
                            if (item.Value.HasValues)
                            {
                                foreach (JProperty itemvalue in item.Value)
                                {
                                    if (itemvalue.Name != "type")
                                    {
                                        txtValue = txtValue + itemvalue.Value.ToString().Replace("\"", "") + " ";
                                    }
                                }
                            }
                            else
                            {
                                txtValue = item.Value.ToString().Replace("\"", "");
                            }
                            Console.WriteLine(txtValue);
                        }
                        else if (item.Name == "position")
                        {
                            positionValue = item.Value.ToString().Replace("\"", "");
                            Console.WriteLine(positionValue);
                        }
                    }
                    int[] positions = new int[8];
                    string[] newMatch = positionValue.Split(',');
                    for (int i = 0; i < newMatch.Length; i++)
                    {
                        positions[i] = Convert.ToInt32(newMatch[i]);
                    }
                    drawRectangleOverWord(positions, txtValue);
                }
            }
            txtValue = "";
            positionValue = "";
        }
    }
}



This code is worked for me in txtvalue I got the Item and In positionvalue I got the Position.

Thanks
Ashish
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900