我通过查询URL
获得了JSON数据http://microblogging.wingnity.com/JSONParsingTutorial/jsonActors
但是当我解析时,我只得到第一组数据,而不是第二组数据,不知道我在哪里弄错了。如何从JSON数组中获取所有节点。谢谢你的帮助。
以下是我的代码:
/**
* 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
}
}
答案 0 :(得分:1)
实际上你必须将所有数据分别设置为一些全局变量,然后在for循环中使用它。 尝试以下代码:
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);
}
这里MyListEntity是全局变量的类。在这里你必须从JSON获取和设置你想要的变量。
答案 1 :(得分:0)
因为在你的Enter row 1: 8 3 9 0 10 => 30
Enter row 2: 3 5 17 1 1 => 27
Enter row 3: 2 8 6 23 1 => 40
Enter row 4: 15 7 3 2 9 => 36
Enter row 5: 6 14 2 6 0 => 28
34 37 37 32 21
循环中,你没有对解析的值做任何事情。在迭代循环之前,您需要将该值存储在某处。
for
答案 2 :(得分:0)
ArrayList<String> weekId;
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String weekId = null;
weekId.add(c.getString("weekid"));
}
答案 3 :(得分:0)
JSON
中以{
开头的任何内容都是JsonObject
,以[
开头的是JsonArray
。在此之后,请参阅以下代码:
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)**/ }
这意味着,obj将是
obj={"weekid":"16","stDate":"20160418","enDate":"20160424","stDay":"Mon","day":["MM","MM","NONE","NONE","NONE","NONE","NONE"],"fbFlag":"-1","twFlag":"-1","pcVpnFlag":"0"}
现在从此对象obj
,您可以使用
String weekid=obj.getString("weekid");
String stDate=obj.getString("stDate");
等等。
答案 4 :(得分:0)
你需要从你的回复中获取Json数组。 只需按照以下步骤操作:
JSONObject json=new JSONObject(result);
JSONArray JArrary= json.getJSONArray("weeklydata");
之后你需要呼叫循环
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();
}
希望它能帮到你:)。
答案 5 :(得分:0)
尝试使用以下内容替换JSONArray:
JSONArray data = json.optJSONArray("weeklydata");
答案 6 :(得分:0)
@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
}
}
公共课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);
}
}