[英]How to get values inside from an array which is inside of an object?
我想從 JSON 中獲取緯度和經度的值,它由兩個對象“stoppage”和“routePlaceback”組成,現在我只能從“routePlaceback”獲取數據,但我不知道如何獲取緯度和經度的值? 代碼如下,
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");
}
}
}
我的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"
}
]
}
這是我運行上述代碼時得到的結果,
但我想要的輸出是,
23.034403, 72.502385
23.034045, 72.506072
您可以在顯示時提取所需的值:
while (iterator_1.hasNext())
{
JSONObject obj = (JSONObject) iterator_1.next()l
System.out.println(obj.get("latitude")+" , "+obj.get("longitude"));
System.out.println("\n");
}
這是您的while循環修改:
while (iterator_1.hasNext()){
JSONObject next = (JSONObject) iterator_1.next();
System.out.print(next.get("latitude") + ", " + next.get("longitude"));
System.out.println("\n");
}
只需獲取iterator_1.next()
返回的對象的“緯度”和“經度”屬性即可。 而且System.out.println();
已經打印了新行, System.out.println("\\n);
實際上打印了兩行。我不知道您是否打算這樣做。
您正在嘗試訪問可獲取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");
}
我修改了一些代碼以實現目標。 請找到以下示例:
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"));
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.