[英]How to get values inside from an array which is inside of an object?
I want to get the values of latitude and longitude from the JSON, which consists of two objects "stoppage" & "routePlaceback", now I'm able to get data from "routePlaceback" only, but I have no clue how to get only the values of latitude and longitude?我想从 JSON 中获取纬度和经度的值,它由两个对象“stoppage”和“routePlaceback”组成,现在我只能从“routePlaceback”获取数据,但我不知道如何获取纬度和经度的值? code is as follows,代码如下,
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class Finder_Json
{
@SuppressWarnings("rawtypes")
public static void main(String[] args) throws Exception
{
// parsing JSON file
Object sampleFile_object = new JSONParser().parse(new FileReader("sample.json"));
// typecasting object to JSONObject
JSONObject sampleFile_JSONObject = (JSONObject) sampleFile_object;
JSONArray routePlaceback = (JSONArray) sampleFile_JSONObject.get("routePlaceback");
Iterator iterator_1 = routePlaceback.iterator();
while (iterator_1.hasNext())
{
System.out.println(iterator_1.next());
System.out.println("\n");
}
}
}
My sample.json
file consists,我的sample.json
文件包括,
{
"stoppage":
[
{
"latitude": "23.074207",
"longitude": "72.557227",
"record_date": 1556217000,
"start_time": 1556217000,
"end_time": 1556304360,
"duration_time": 1456
}
],
"routePlaceback":
[
{
"distance": 0.36,
"longitude": "72.502385",
"ignition": 1,
"record_date": 1556303400,
"speed": 53.708,
"latitude": "23.034403"
},
{
"distance": 0.38,
"longitude": "72.506072",
"ignition": 1,
"record_date": 1556303430,
"speed": 25.927999,
"latitude": "23.034045"
}
]
}
This is what I get when I run the above code,这是我运行上述代码时得到的结果,
But my desired output is as,但我想要的输出是,
23.034403, 72.502385
23.034045, 72.506072
You can extract the required values while displaying: 您可以在显示时提取所需的值:
while (iterator_1.hasNext())
{
JSONObject obj = (JSONObject) iterator_1.next()l
System.out.println(obj.get("latitude")+" , "+obj.get("longitude"));
System.out.println("\n");
}
Here is your while loop modified : 这是您的while循环修改:
while (iterator_1.hasNext()){
JSONObject next = (JSONObject) iterator_1.next();
System.out.print(next.get("latitude") + ", " + next.get("longitude"));
System.out.println("\n");
}
Just get "latitude" and "longitude" property of object that iterator_1.next()
returns. 只需获取iterator_1.next()
返回的对象的“纬度”和“经度”属性即可。 Moreover System.out.println();
而且System.out.println();
prints new line already, System.out.println("\\n);
actually prints two new lines. I don't know if it was intended by you. 已经打印了新行, System.out.println("\\n);
实际上打印了两行。我不知道您是否打算这样做。
You are trying to access the property of an Object
where you can get the Object
. 您正在尝试访问可获取Object
property of an Object
的property of an Object
。
while (iterator_1.hasNext())
{
JSONObject k= (JSONObject)iterator_1.next()
System.out.println(k.latitude+" "+k.longitude);
System.out.println("\n");
}
I modified a little bit your code to achieve your target. 我修改了一些代码以实现目标。 Please find the example below: 请找到以下示例:
import java.io.FileReader;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class Finder_Json
{
@SuppressWarnings("rawtypes")
public static void main(String[] args) throws Exception
{
// parsing JSON file
Object sampleFile_object = new JSONParser().parse(new FileReader("src/main/resources/sample.json"));
// typecasting object to JSONObject
JSONObject sampleFile_JSONObject = (JSONObject) sampleFile_object;
JSONArray routePlaceback = (JSONArray) sampleFile_JSONObject.get("routePlaceback");
Iterator iterator = routePlaceback.iterator();
while (iterator.hasNext()) {
JSONObject objt = (JSONObject) iterator.next();
System.out.println(objt.get("latitude") + ", " + objt.get("longitude"));
}
}
}
I strongly suggest to use Other libraries like GSON or Jackson anyway. 我强烈建议无论如何都要使用其他库,例如GSON或Jackson 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.