我有以下json对象
{
"result": {
"today": [],
"week": [{
"ride_id": "23829994",
"trip_average_speed": "46.15",
"trip_distance": "2.20002797646063",
"trip_time": "2014-02-13 06:39:30+00"
}, {
"ride_id": "15761690",
"trip_average_speed": "32.09",
"trip_distance": "16.3443284767516",
"trip_time": "2014-02-13 11:40:30+00"
}],
"month": []
}
}
如何将"today": []
,"month": []
和周值作为数组?
答案 0 :(得分:0)
查看google-json及其user-guide。除非你特别想要杰克逊的东西。
希望这会帮助你开始
答案 1 :(得分:0)
JSONObject obj = new JSONObject(" .... ");
JSONArray arr = obj.getJSONArray("week");
for (int i = 0; i < arr.length(); i++)
{
String ride_id = arr.getJSONObject(i).getString("ride_id");
String trip_average_speed = arr.getJSONObject(i).getString("trip_average_speed");
String trip_distance = arr.getJSONObject(i).getString("trip_distance");
String trip_time = arr.getJSONObject(i).getString("trip_time");
System.out.println("ride_id");
}
您可以使用以上答案获取周数据。使用名为org.json的库。
谢谢..
答案 2 :(得分:0)
使用Google gson库,您可以轻松地在Java中处理Json。请参考Userguide,您可以从here
下载jar尝试以下代码从Json中检索"today": [], "month": [] and "week": []
数组。
String jsonStr = "your json string";
Gson gson = new Gson();
JsonObject jsonObj = gson.fromJson (jsonStr, JsonElement.class).getAsJsonObject;
//--------------- Getting week[] as Arraylist ------------------------
public class Week
{
private long ride_id;
private double trip_average_speed;
private double trip_distance;
private String trip_time;
//Setters and Getters for all the parameters
}
ArrayList<Week> returnedArtworks = gson.fromJson(jsonObj.get("week").getAsString(), new TypeToken<List<Week>>(){}.getType());
//---------------- Getting month[] as Arraylist -----------------------
public class Month
{
//Parameters
//Setters and Getters for all the parameters
}
ArrayList<Month> returnedArtworks = gson.fromJson(jsonObj.get("month").getAsString(), new TypeToken<List<Month>>(){}.getType());
//----------------- Getting today[] as Arraylist ----------------------
public class Today
{
//Parameters
//Setters and Getters for all the parameters
}
ArrayList<Today> returnedArtworks = gson.fromJson(jsonObj.get("today").getAsString(), new TypeToken<List<Today>>(){}.getType());
答案 3 :(得分:-1)
一种方法是为today
,week
,month
和result
创建POJO。 result
课程将包含today
,week
和month
所有其他三个的列表。
创建一个包含result
类对象的主POJO MyJson
。现在您可以直接将此JSON解析到您的类中,并从getter方法中获取today
,{{1}列表}和week
数组
要将JSON字符串解析为类对象:
month
您的POJO课程如下:
MyJson.java
ObjectMapper mapper = new ObjectMapper();
MyJson resultObj = mapper.readValue(str, MyJson.class);
//get List of week
List<Week> week = resultObj.getResult().getWeek();
Result.java
public class MyJson
{
private Result result;
//getter and setter method
}
Today.java
public class Result
{
private List<Today> today;
private List<Month> month;
private List<Week> week;
//getter and setter method
}
Week.java
public class Today
{
private String date;
//other parameters and it's getter and setter method.
}
Month.java
public class Week
{
private long ride_id;
private double trip_average_speed;
//other parameters and it's getter and setter method.
}