Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/179.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android中解析JSON数据_Java_Android_Json - Fatal编程技术网

Java 在Android中解析JSON数据

Java 在Android中解析JSON数据,java,android,json,Java,Android,Json,我有一个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":

我有一个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应用程序设置了谷歌地图,所以我有一个在两个活动之间传递名称值(GoogleMaps.java和WeatherInfo.java),现在当我单击谷歌地图中的一个点时,它会将名称传递到WeatherInfo.java,我想获取该名称的天气数据

例如:我单击地图上的法国点,WeatherInfo.class将得到名称为“France”,并打印出该点的“日期、时间、露点、气温”

我的问题是,如何才能只解析我在地图中单击的点的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;
    }
受保护的Void doInBackground(Void…arg0){
//创建服务处理程序类实例
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
contacts=jsonObj.getJSONArray(“”);
//通过所有触点循环
对于(int i=0;ivalue
联系人:put(标签名称、姓名);
联系人。放置(标记日期、日期和时间);
触点放置(标签温度、温度);
接触。放置(标记露点、露点);
//将联系人添加到联系人列表
联系人列表。添加(联系人);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
返回null;
}
JSONArray数组=(JSONArray)新的JSONTokener(jsonStr).nextValue();

对于(int i=0;i您可以使用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;
    }

}
然后这个Jackson对象映射器

// 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());            
}

我如何才能得到仅针对我在地图中单击的点解析的Json数据?只需使用库解析所有Json,并在需要时从解析结果中检索相关数据。联系人列表是否包含所有位置以及日期,即法国、英国等我使用的库,只是认为我的for循环不正确,可以吗看我在类中的for循环吗?是的,网页上显示的contec列表是我在上面放的,只有一个数组,包含这三个点名称,日期时间,露点,空气温度。你的json以JsonArray[]开始,而不是JSONObject{}你是对的,我是按照下面的链接来做这门课的,我想实现一些东西,但仅仅因为我的json数据格式与教程不同,我就迷路了。嗨@vrbsm,我怎么说如果我从GoogleMaps获得的数据是法国,而不是我想打印name=“France”的天气信息(日期、时间、露点、气温)只是,我怎样才能做到呢?
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());            
}