在 Java 中的 Json 对象中获取 Json 对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27286036/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 04:06:36  来源:igfitidea点击:

Getting Json object inside a Json object in Java

javajson

提问by Exception

So I have some code that is able to send this out:

所以我有一些代码可以将其发送出去:

  {"id":1,
   "method":"addWaypoint",
   "jsonrpc":"2.0",
   "params":[
     {
       "lon":2,
       "name":"name",
       "lat":1,
       "ele":3
     }
    ]
   }

The server receives this JSON object as a string named "clientstring":

服务器接收此 JSON 对象作为名为“clientstring”的字符串:

 JSONObject obj = new JSONObject(clientstring); //Make string a JSONObject
 String method = obj.getString("method"); //Pulls out the corresponding method

Now, I want to be able to get the "params" value of {"lon":2,"name":"name","lat":1,"ele":3} just like how I got the "method". however both of these have given me exceptions:

现在,我希望能够获得 {"lon":2,"name":"name","lat":1,"ele":3} 的“params”值,就像我获得“方法”一样”。然而,这两个都给了我例外:

String params = obj.getString("params");

and

 JSONObject params = obj.getJSONObject("params");

I'm really at a loss how I can store and use {"lon":2,"name":"name","lat":1,"ele":3} without getting an exception, it's legal JSON yet it can't be stored as an JSONObject? I dont understand.

我真的不知道如何存储和使用 {"lon":2,"name":"name","lat":1,"ele":3} 没有例外,它是合法的 JSON 但它不能存储为 JSONObject?我不明白。

Any help is VERY appreciated, thanks!

非常感谢任何帮助,谢谢!

采纳答案by Lokesh

paramsin your case is nota JSONObject, but it is a JSONArray.

params你的情况是不是一个JSONObject的,但它是一个JSONArray

So all you need to do is first fetch the JSONArrayand then fetch the first element of that array as the JSONObject.

因此,您需要做的就是首先获取JSONArray,然后获取该数组的第一个元素作为JSONObject

JSONObject obj = new JSONObject(clientstring); 
JSONArray params = obj.getJsonArray("params");
JSONObject param1 = params.getJsonObject(0);

回答by Wundwin Born

How try like that

怎么试试

    JSONObject obj = new JSONObject(clientstring);
    JSONArray paramsArr = obj.getJSONArray("params");


    JSONObject param1 = paramsArr.getJSONObject(0);

    //now get required values by key
    System.out.println(param1.getInt("lon"));
    System.out.println(param1.getString("name"));
    System.out.println(param1.getInt("lat"));
    System.out.println(param1.getInt("ele"));

回答by incr3dible noob

Here "params" is not an object but an array. So you have to parse using:

这里的“params”不是一个对象而是一个数组。所以你必须解析使用:

JSONArray jsondata = obj.getJSONArray("params");

for (int j = 0; j < jsondata.length(); j++) {
    JSONObject obj1 = jsondata.getJSONObject(j);
    String longitude = obj1.getString("lon");
    String name = obj1.getString("name");
    String latitude = obj1.getString("lat");
    String element = obj1.getString("ele");
  }