我有JSON响应,想要动态获取项目和位置值。 [英] I have JSON response and want to get the item and position value dynamically.

查看:64
本文介绍了我有JSON响应,想要动态获取项目和位置值。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

我写了一篇关于这样的问题的文章:在C#中使用JSON& VB [ ^ ] ...它将在JSON反序列化中为您回答所有问题
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


这是一个很大的JSON。我通过json linter运行它( JSONLint - JSON验证器 [ ^ ])并且它无效,因此更难以给你快速回答。



所以,我抓住它的第一部分引导你找到解决方案:



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:

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





如果您要执行以下操作,您可以自己解决这个问题:

1)打开浏览器开发工具(通常是F12)

2)复制上面的json。

3)在dev工具命令控制台中键入以下内容:



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:

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

var address =



然后粘贴在json中然后按< enter>



现在你可以自己检查一下地址对象。



在开发控制台中,键入以下内容并按< enter>




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>

address[0].item.country





当你这样做,控制台将显示:USA

这是一个快照,它显示了它的样子,在我输入这个答案的同时在这个(CP)页面的底部运行。 />
https://i.stack.imgur.com/ItMXd.png [ ^ ]



现在你可以修复你的JSON并设置你的对象并亲自试试。



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.


感谢你们两位的宝贵答案。

这是我用来修复此问题的方法。



因为我对每个块的项目和位置感兴趣。





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







这个代码在txtvalue中为我工作我得到了Item和In positionvalue我得到了位置。



谢谢

Ashish




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

Thanks
Ashish


这篇关于我有JSON响应,想要动态获取项目和位置值。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆