【发布时间】:2017-07-04 05:06:22
【问题描述】:
{"Suggestions":[{"Itinerary_1":[{"Cities":"Madurai - Pazhamudir Cholai (Madurai) - Pillayarpatti - Chennai","Citieids":"14-85-114-2","Kms":595}],"Itinerary_2":[{"Cities":"Madurai - Pillayarpatti - Pillayarpatti - Chennai","Citieids":"14-114-114-2","Kms":560}],"Itinerary_3":[{"Cities":"Madurai - Pillayarpatti - Pillayarpatti - Chennai","Citieids":"14-114-114-2","Kms":560}]}]}
这是我的 json 响应。这个 json 有一个内部数组我如何获取值
这是我的代码:
class itinerarydetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
*/
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("orgin", "14"));
params.add(new BasicNameValuePair("destination", "2"));
params.add(new BasicNameValuePair("orgin_date", "28/06/2017"));
params.add(new BasicNameValuePair("destination_date", "07/07/2017"));
json = jsonParser.makeHttpRequest(itineraryurl, "GET",
params);
try {
//Suggestions
jsonarray = json.getJSONArray("Suggestions");
// JSONArray list = mainObj.getJSONArray("prodCat_list");
String check;
if(jsonarray != null) {
// Toast.makeText(getApplicationCon text(), "MAIN ARRAY IS NOT EMPTY ITS HAVE A VALUE",
// Toast.LENGTH_SHORT).show();
Log.d("check","MAIN ARRAY IS NOT EMPTY ITS HAVE A VALUE");
for (int i = 0; i < jsonarray.length(); i++) {
// JSONObject elem = list.getJSONObject(i);
JSONObject object = jsonarray.getJSONObject(i);
//JSONArray prods = elem.getJSONArray("prods");
jsoninsidearray = object.getJSONArray("Itinerary_1");
if(jsoninsidearray != null) {
Log.d("check","INSIDE ARRAY IS NOT EMPTY ITS HAVE A VALUE");
for (int j = 0; j < jsoninsidearray.length(); j++) {
JSONObject innerElem = jsoninsidearray.getJSONObject(j);
if(innerElem != null) {
Log.d("check","innerElement ARRAY IS NOT EMPTY ITS HAVE A VALUE");
cityjson = innerElem.getString("Cities");
itinerary = innerElem.getString("Citieids");
totalkms = innerElem.getString("Kms");
}
else
{
Log.d("check","innerElement ARRAY IS EMPTY");
}
}
}
else
{
Toast.makeText(getApplicationContext(), "INSIDE ARRAY IS EMPTY",
Toast.LENGTH_SHORT).show();
Log.d("check","INSIDE ARRAY IS EMPTY");
}
}
}
else
{
Toast.makeText(getApplicationContext(), "MAIN ARRAY IS EMPTY",
Toast.LENGTH_SHORT).show();
Log.d("check","MAIN ARRAY IS EMPTY");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
Toast.makeText(getApplicationContext(), "hai",
Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), cityjson, Toast.LENGTH_SHORT).show();
}
}
我是 Vignesh,我有一个疑问,所以请在下面提供帮助 但是显示了空值。如何从 json 中获取内部数组中的值。
【问题讨论】:
-
我认为这回答了你的问题:stackoverflow.com/questions/17673057/…
-
在此,有一个建议数组,其中有三个不同的 Array("Itinerary_1","Itinerary_2","Itinerary_3") 。但是你给出的答案有 (prodCat_list) 数组和它有一个 Multiple 数组,但所有内容都具有相同的名称,因此可以轻松调用。我也尝试过这个想法,但它显示的是空值。
-
object.getJSONArray("Itinerary_"+ (i+1)) 是完美的,因为没有“Itinerary_0”。比它我会停止解析亲爱的。检查我的答案
标签: android