简体   繁体   English

如何从对象内部的数组中获取内部值?

[英]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 Objectproperty 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. 我强烈建议无论如何都要使用其他库,例如GSONJackson

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从数组内的对象中获取值? - How to get value from an object which is inside an array? 如何获取JSON数组内部的JSON对象 - How do I get the JSON object which is inside of JSON array 如何从for循环中的textfield获取多个值并将其重新放入数组中 - how to get multiple values from textfield inside a for loop and recive it in an array 从存储在地图内的数组中获取值 - Get values from an array stored inside a map 如何调用对象内部的json数组并且该对象位于数组中? - How to call a json array which is inside a object and that object is in side a array? 如何获取ArrayList内部对象的属性和值? - How to get the atributes and values of an object that is inside an ArrayList? 如何从向量内部的数组中读取数据? - How to read from an array which is inside a vector? 如何访问位于另一个对象内部的对象数组,该对象然后位于超类数组内部? - How can I access an object array located inside of another object which is then inside a superclass array? 如何获取StringBuilder中的字符数组以避免数组复制 - How to get character array which is inside StringBuilder to avoid array copy 如何打印java中arraylist内的pojo对象的值? - How to print the values of the pojo object which is inside an arraylist in java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM