繁体   English   中英

在Android中解析JSON数据

[英]parse JSON data in Android

我有以下格式的JSON数据,

[
    {
        "name": "France",
        "date_time": "2015-05-17 19:59:00",
        "dewpoint": "17",
        "air_temp": "10.8"
    },
    {
        "name": "England",
        "date_time": "2015-05-17 19:58:48",
        "dewpoint": "13",
        "air_temp": "10.6"
    },
    {
        "name": "Ireland",
        "date_time": "2015-05-17 19:58:50",
        "dewpoint": "15",
        "air_temp": "11.1"
    }
]

我已经为Android应用程序设置了一个Google地图,因此我在两个活动(GoogleMaps.java和WeatherInfo.java)之间传递了名称值,现在当我单击Google Map中的一个点时,它将名称传递给WeatherInfo.java,我想获取该名称的天气数据。

例如:我在地图上单击“法国”点,WeatherInfo.class的名称将为“ France”,并打印出该点的“ date_time,露点,air_temp”。

我的问题是我如何才能仅对我在地图中单击的点进行解析的Json数据? 谁能看到我的WeatherInfo.java类中的for循环?

WeatherInfo.java

protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray("");

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);


                    String name = c.getString(TAG_NAME);
                    String date_time = c.getString(TAG_DATE);
                    String temp = c.getString(TAG_TEMP);
                    String dewpoint = c.getString(TAG_DEWPOINT);

                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_NAME, name);
                    contact.put(TAG_DATE, date_time);
                    contact.put(TAG_TEMP, temp);
                    contact.put(TAG_DEWPOINT, dewpoint);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }
JSONArray array = (JSONArray)new JSONTokener(jsonStr).nextValue();

for(int i = 0; i<array.length(); i++){
    JSONObject jsonObject = array.getJSONObject(i);             
    String name = jsonObject.getString("name");
    String date = jsonObject.getString("date_time");
    ...
}

要么

JSONArray array = (JSONArray)new JSONTokener(jsonStr).nextValue();

for(int i = 0; i<array.length(); i++) {
    JSONObject jsonObject = array.getJSONObject(i);             
    City city = new City();
    city.name = jsonObject.getString("name");        
    ...
}

您可以按以下方式使用Jackson JSON解析器:-

您将需要一个用于点数据的值对象。

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Point {

    private final String name;
    private final String dateTime;
    private final int dewpoint;
    private final double airTemp;

    @JsonCreator
    public Point(@JsonProperty("name") final String name, @JsonProperty("date_time") final String dateTime, @JsonProperty("dewpoint") final int dewpoint, @JsonProperty("air_temp") final double airTemp) {
        this.name = name;
        this.dateTime = dateTime;
        this.dewpoint = dewpoint;
        this.airTemp = airTemp;
    }

    public String getName() {
        return name;
    }

    public String getDateTime() {
        return dateTime;
    }

    public int getDewpoint() {
        return dewpoint;
    }

    public double getAirTemp() {
        return airTemp;
    }

}

然后这个杰克逊对象映射器

// 2. Convert JSON to Java object
ObjectMapper mapper = new ObjectMapper();
Point[] points = mapper.readValue(new File("points.json"), Point[].class);

for (Point point : points) {
    System.out.println("" + point.getName());
    System.out.println("" + point.getDateTime());
    System.out.println("" + point.getDewpoint());
    System.out.println("" + point.getAirTemp());            
}

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM