Reputation: 1514
I am getting below JSON data by querying URL
http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors
But when I parse, I am only getting first set of data and not second one, not sure where am I making mistake. How to get all nodes from JSON array. Thanks for your help.
Below is my code :
/**
* getting Inbox JSON
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
url = "http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors"
Log.d(TAG , "URL IS:" + url);
JSONObject json = jsonParser.makeHttpRequest(url, "GET", params, false);
Log.d("Parse", "Data : " + json.toString());
try {
data = json.getJSONArray("actors");
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
//Here I am getting all values one by one
}
} catch (JSONException e) {
e.printStackTrace();
} catch(Exception e){
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
}
}
Upvotes: 0
Views: 370
Reputation: 1662
@Override
protected void onPreExecute() {
super.onPreExecute();
}
/**
* getting Inbox JSON
*/
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
url = "www.someurl.com"
Log.d(TAG, "URL IS:" + url);
JSONObject json = jsonParser.makeHttpRequest(url, "GET", params, false);
try {
JSONArray data = json.getJSONArray("weeklydata");
//I am concerned here. Here it is only giving first set of data (week16) and not second one (week15)
List<Weaklydata> list = new ArrayList<>();
for (int a = 0; a < data.length(); a++) {
Weaklydata weaklydata = new Weaklydata();
JSONObject jsonObject1 = data.getJSONObject(a);
weaklydata.setEnDate(jsonObject1.getString("enDate"));
weaklydata.setFbFlag(jsonObject1.getString("fbFlag"));
weaklydata.setTwFlag(jsonObject1.getString("pcVpnFlag"));
weaklydata.setStDate(jsonObject1.getString("stDate"));
weaklydata.setStDay(jsonObject1.getString("stDay"));
weaklydata.setWeekid(jsonObject1.getString("twFlag"));
weaklydata.setWeekid(jsonObject1.getString("weekid"));
List<String> dayList = new ArrayList<>();
JSONArray jsonArray = jsonObject1.getJSONArray("day");
for (int pos = 0; pos < jsonArray.length(); pos++) {
dayList.add(jsonArray.getString(pos));
}
weaklydata.setDay(dayList);
list.add(weaklydata);
}
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
}
}
public class Weaklydata {
private String weekid;
private String stDate;
private String enDate;
private String stDay;
private List<String> day = new ArrayList<String>();
private String fbFlag;
private String twFlag;
private String pcVpnFlag;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
/**
* @return The weekid
*/
public String getWeekid() {
return weekid;
}
/**
* @param weekid The weekid
*/
public void setWeekid(String weekid) {
this.weekid = weekid;
}
/**
* @return The stDate
*/
public String getStDate() {
return stDate;
}
/**
* @param stDate The stDate
*/
public void setStDate(String stDate) {
this.stDate = stDate;
}
/**
* @return The enDate
*/
public String getEnDate() {
return enDate;
}
/**
* @param enDate The enDate
*/
public void setEnDate(String enDate) {
this.enDate = enDate;
}
/**
* @return The stDay
*/
public String getStDay() {
return stDay;
}
/**
* @param stDay The stDay
*/
public void setStDay(String stDay) {
this.stDay = stDay;
}
/**
* @return The day
*/
public List<String> getDay() {
return day;
}
/**
* @param day The day
*/
public void setDay(List<String> day) {
this.day = day;
}
/**
* @return The fbFlag
*/
public String getFbFlag() {
return fbFlag;
}
/**
* @param fbFlag The fbFlag
*/
public void setFbFlag(String fbFlag) {
this.fbFlag = fbFlag;
}
/**
* @return The twFlag
*/
public String getTwFlag() {
return twFlag;
}
/**
* @param twFlag The twFlag
*/
public void setTwFlag(String twFlag) {
this.twFlag = twFlag;
}
/**
* @return The pcVpnFlag
*/
public String getPcVpnFlag() {
return pcVpnFlag;
}
/**
* @param pcVpnFlag The pcVpnFlag
*/
public void setPcVpnFlag(String pcVpnFlag) {
this.pcVpnFlag = pcVpnFlag;
}
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
Upvotes: 0
Reputation: 29
Actually you have to set all the data separately to the some global variables and then use that in for loop. try following code:
ArrayList<MyListEntity> subList;
for (int i = 0; i < data.length(); i++) {
//get the information JSON object
String subIDInfo = objSub.getJSONObject(i).toString();
//create java object from the JSON object
MyListEntity cat = gson.fromJson(subIDInfo, MyListEntity.class);
//add to country array list
subList.add(cat);
}
here MyListEntity is the class of that global variables.In this you have to get and set the variables you want from JSON.
Upvotes: 1
Reputation: 286
Try Replacing your JSONArray with below:
JSONArray data = json.optJSONArray("weeklydata");
Upvotes: 0
Reputation: 1971
You need to fatch Json Array from your response.. Just Follow this Step:
JSONObject json=new JSONObject(result);
JSONArray JArrary= json.getJSONArray("weeklydata");
After that You need to Call for loop
for (int i= 0; i< JArrary.size(); i++) {
JSONObject obj= JArrary.getJSONObject(JArrary);
String weekid = obj.getString("weekid");
String stDate = obj.getString("stDate");
String enDate = obj.getString("enDate");
String stDay = obj.getString("stDay");
String fbFlag = obj.getString("fbFlag");
String twFlag = obj.getString("fbFlag");
String pcVpnFlag = obj.getString("pcVpnFlag");
JSONArray newArray= obj.getJSONArray("day");
String Day = newArray.toString();
}
Hope its Help you :)
Upvotes: 0
Reputation: 684
in JSON
anything beginning with a {
is a JsonObject
and beginning with [
is a JsonArray
. Following this, refer below code:
jsonArray=fullJsonObject.getJsonArray("weeklydata");
for(int i=0;i<jsonArray.length();i++){
JSONObject obj=jsonArray.getJsonObject(i);
/**this will give you the first object for i= 0 now (see below)**/ }
that means that, obj will be
obj={"weekid":"16","stDate":"20160418","enDate":"20160424","stDay":"Mon","day":["MM","MM","NONE","NONE","NONE","NONE","NONE"],"fbFlag":"-1","twFlag":"-1","pcVpnFlag":"0"}
now from this object obj
, you can retrieve all strings using
String weekid=obj.getString("weekid");
String stDate=obj.getString("stDate");
and so on.
Upvotes: 0
Reputation: 1
ArrayList<String> weekId;
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String weekId = null;
weekId.add(c.getString("weekid"));
}
Upvotes: 0
Reputation: 1457
Because in your for
loop you are not doing anything with the parsed value. You need to store that value somewhere before iterating the loop.
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String weekId = null;
weekId = c.getString("weekid"); //store this value in a String array or ArrayList
}
Upvotes: 0