我要翻译的内容: 如何从我的JSON响应中提取地图?

huangapple go评论62阅读模式
英文:

How can I extract Map from my JSON respone

问题

JSON响应:

{
    "took": 24,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.0,
        "hits": [
            {
                "_index": "alibaba",
                "_type": "alibaba",
                "_id": "[\"523486f0-aaaa-aaaa-bdc8-a39572623db\",\"test\",\"cloud\"]",
                "_score": 0.0,
                "_source": {
                    "Company": "alibaba",
                    "myMap": {
                        "Key_1": "Value_1",
                        "Key_2": "Value_2",
                        "Key_3": "Value_3"
                    }
                }
            }
        ]
    }
}

从上面,我需要提取出地图("myMap")。我所做的如下:

JSONObject response = (JSONObject) element.get("_source");
String company = response.optString("Company", null);
// 到这里的代码是正确的。但在下一行会报错。
String company = response.optString("myMap");

我知道我试图将地图提取为字符串,这是问题的原因。

因此,我想知道如何从JSON对象中检索“myMap”。

谢谢。

英文:

JSON Response:

{
    "took": 24,
    "timed_out": false,
    "_shards": {
        "total": 3,
        "successful": 3,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 0.0,
        "hits": [
            {
                "_index": "alibaba",
                "_type": "alibaba",
                "_id": "[\"523486f0-aaaa-aaaa-bdc8-a39572623db\",\"test\",\"cloud\"]",
                "_score": 0.0,
                "_source": {
                      "Company" : "alibaba",
                      "myMap": {
                          "Key_1": "Vlaue_1",
                          "Key_2": "Vlaue_2",
                          "Key_3": "Vlaue_3",
                         }
                }
            }
        ]
 }   

From the above, I need to extract the map.
What I did is as follows.

JSONObject response= (JSONObject) element.get("_source");
String company = response.optString("Company",null);
// Till this code works fine. But gives error in next line.
String company = response.optString("myMap");

I know I am trying to get map in string, which is cause of problem.

Hence, I want to know how can I retrieve 'myMap' from the JSON Object.

Thanks.

答案1

得分: 0

你应该能够提取JSONObject,不确定你正在使用哪个库,但是你应该能够提取JSONObject

JSONObject response= (JSONObject) element.get(" _source ");
JSONObject company = (JSONObject) response.get(" myMap ");
英文:

Not sure which library you are using but, you should be able to extract the JSONObject

JSONObject response= (JSONObject) element.get("_source");
JSONObject company = (JSONObject) response.get("myMap");

答案2

得分: 0

首先,提供的 JSON 不是有效的,我不知怎么使其工作并使用了你提供的整个 JSON。

JSONObject myMap = obj.getJSONObject("hits").getJSONArray("hits").getJSONObject(0).getJSONObject("_source").getJSONObject("myMap");

如果你想要一个映射(map),你可以使用一个映射器(mapper)将 jsonObject 转换为映射,就像这个链接中所示:https://stackoverflow.com/questions/443499/convert-json-to-map

英文:

First of all the json provided is not valid I somehow made it work and used the entire json you provided.

    JSONObject myMap = obj.getJSONObject("hits").getJSONArray("hits").getJSONObject(0).getJSONObject("_source").getJSONObject("myMap");

If you want a map you can use a mapper to change jsonObject to map as in https://stackoverflow.com/questions/443499/convert-json-to-map

huangapple
  • 本文由 发表于 2020年8月7日 12:04:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/63295025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定