0

i have the following json object

{
    "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": []
    }
}

How can I get "today": [], "month": [] and week values as arrays?

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Sush
  • 1,449
  • 8
  • 26
  • 51

4 Answers4

0

Have a look at google-json and its user-guide. Unless you want something jackson specifically.

Hope this would help you get going

black sensei
  • 6,528
  • 22
  • 109
  • 188
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");
}

you can get week data using above answer.. Using library called org.json.

Thanks..

Java Man
  • 1,854
  • 3
  • 21
  • 43
0

Using Google gson library, you can easily handle Json in Java. Please refer Userguide and you can download jar from here

Try below code to retrieve "today": [], "month": [] and "week": [] arrays from your Json.

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());
Purushotham
  • 3,770
  • 29
  • 41
-1

One way is to create POJO for your today, week, month and result. result class will include list of the all other three that is today, week and month.
Create one master POJO that contain your result class object say MyJson.Now you can directly parse this JSON into your class and from the getter method you have list of today,week and month array.
To parse you JSON string into class object :

ObjectMapper mapper = new ObjectMapper();
MyJson resultObj = mapper.readValue(str, MyJson.class);  
//get List of week  
List<Week> week =  resultObj.getResult().getWeek();    

Your POJO classes look like :
MyJson.java

public class MyJson   
{
     private Result result;  
     //getter and setter method  
}  

Result.java

public class Result   
{
    private List<Today> today;
    private List<Month> month;
    private List<Week> week;    
    //getter and setter method
}  

Today.java

public class Today   
{
  private String date;  
  //other parameters and it's getter and setter method.  
}  

Week.java

public class Week   
{
    private long ride_id;
    private double trip_average_speed;  
    //other parameters and it's getter and setter method.    
}   

Month.java

public class Month   
{
   private String monthName;    
  //other parameters and it's getter and setter method.      
}  
Yagnesh Agola
  • 4,556
  • 6
  • 37
  • 50