获取JSON对象或Map的参数

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

Get Parameters of JSON Object or a Map<String, String>

问题

如何获取像"firstname"、"lastname"等元素,如果它们的格式如下:

{     "count" : 2,

     "elements" : [

         {             "firstname" : "XXXXXXXXXX",             "lastname" : "A22"         },

         {             "firstname" : "YYYYYYYYYY",             "lastname" : "A23"         }     

]

}

还有如何获取elements[0]和elements1呢?这是因为我想验证每个参数中的内容。

-重要-

我的元素来自以Json格式的API,我将其解析为Map<String, String>,所以我只需要一个Json格式或Map<String, String>格式的解决方案。

我尝试使用userDetails.get("elements")1,以及userDetails.get("elements").[SOMETHING],但没有任何效果,自动补全中也没有得到任何提示...

英文:

how do I get the elements like "firstname", "lastname", etc. If they are in this format:

{     &quot;count&quot; : 2,

     &quot;elements&quot; : [

         {             &quot;firstname&quot; : &quot;XXXXXXXXXX&quot;,             &quot;lastname&quot; : &quot;A22&quot;         },

         {             &quot;firstname&quot; : &quot;YYYYYYYYYY&quot;,             &quot;lastname&quot; : &quot;A23&quot;         }     

]

}

And how do I get elements[0], and elements1?
This is because I want to validate the content in each parameter.

-Important-

My elements are coming form an API in Json Format, and I parse into Map<String, String> so I just need a solution to either a Json format or a Map<String, String> format.

I try using userDetails.get("elements")1, and userDetails.get("elements").[SOMETHING] but anything worked, and I didnt get anything in the autocompletion...

答案1

得分: 1

你可以使用 JSON 库,比如 json-simple

JSONParser parser = new JSONParser();
String jsonData = "{\"count\":2,\"elements\":[{\"firstname\":\"XXXXXXXXXX\",\"lastname\":\"A22\"},{\"firstname\":\"YYYYYYYYYY\",\"lastname\":\"A23\"}]}";
Object obj = parser.parse(jsonData);
// 一个 JSON 对象。键值对是无序的。JSONObject 支持 java.util.Map 接口。
JSONObject jsonObject = (JSONObject) obj;
// 一个 JSON 数组。JSONObject 支持 java.util.List 接口。
JSONArray elements = (JSONArray) jsonObject.get("elements");
elements.forEach(e -> {
    JSONObject element = (JSONObject) e;
    System.out.println(element.get("lastname"));
});
英文:

You can use a JSON library such as json-simple

    JSONParser parser = new JSONParser();
    String jsonData = &quot;{\&quot;count\&quot;:2,\&quot;elements\&quot;:[{\&quot;firstname\&quot;:\&quot;XXXXXXXXXX\&quot;,\&quot;lastname\&quot;:\&quot;A22\&quot;},{\&quot;firstname\&quot;:\&quot;YYYYYYYYYY\&quot;,\&quot;lastname\&quot;:\&quot;A23\&quot;}]}&quot;;
    Object obj = parser.parse(jsonData);
    // A JSON object. Key value pairs are unordered. JSONObject supports java.util.Map interface.
    JSONObject jsonObject = (JSONObject) obj;
    // A JSON array. JSONObject supports java.util.List interface.
    JSONArray elements = (JSONArray) jsonObject.get(&quot;elements&quot;);
    elements.forEach(e -&gt;{
        JSONObject element = (JSONObject) e;
        System.out.println(element.get(&quot;lastname&quot;));
    });

huangapple
  • 本文由 发表于 2023年2月24日 00:25:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547587.html
匿名

发表评论

匿名网友

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

确定