我需要将我的JSON解析到我的Android应用程序,我收到一个错误:
org.json.array无法转换为jsonobject
我想要做的是从我的服务器中获取json并将其解析为我使用AsyncHttpClient创建的textviews,这是我的代码。
AsyncHttpClient client1 = new AsyncHttpClient();
client1.get("http://mahmoudfa-001-site1.atempurl.com/appetizers.json",new
TextHttpResponseHandler() {
@Override
public void onFailure ( int statusCode, Header[] headers, String responseString, Throwable throwable){
}
@Override
public void onSuccess ( int statusCode, Header[] headers, String responseString){
Log.i("a1", responseString);
//testing if the server responding.
Toast.makeText(getApplicationContext(), responseString, Toast.LENGTH_LONG).show();
try {
JSONObject job = new JSONObject(responseString);
JSONArray arr = new JSONArray(build);
//String arrlen = Integer.toString(arr.length());
JSONObject na = arr.getJSONObject(0);
JSONArray ingna = na.getJSONArray("unavailable");
String[] ingr = new String[ingna.length()];
for (int k = 0; k < ingna.length(); k++) {
JSONObject abc = ingna.getJSONObject(k);
ingr[k] = abc.getString("ingredient");
}
for (int i = 1; i < arr.length(); i++) {
JSONObject food = null;
food = arr.getJSONObject(i);
String name = food.getString("name");
String description = food.getString("description");
String rating = food.getString("rating");
String price = food.getString("price");
String cooktime = food.getString("cooktime");
JSONArray ingredients = food.getJSONArray("ingredients");
String[] ing = new String[ingredients.length()];
for (int k = 0; k < ingredients.length(); k++) {
JSONObject ingd = ingredients.getJSONObject(k);
ing[k] = ingd.getString("ingredient");
}
for (int l = 0; l < ing.length; l++) {
for (int m = 0; m < ingr.length; m++) {
if (ing[l].matches(ingr[m])) ;
}
}
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
JSONObject job = new JSONObject(responseString);
JSONArray arr = new JSONArray(build);
//String arrlen = Integer.toString(arr.length());
JSONObject na = arr.getJSONObject(0);
JSONArray ingna = na.getJSONArray("unavailable");
String[] ingr = new String[ingna.length()];
for (int k = 0; k < ingna.length(); k++) {
JSONObject abc = ingna.getJSONObject(k);
ingr[k] = abc.getString("ingredient");
}
for (int i = 1; i < arr.length(); i++) {
JSONObject food = null;
food = arr.getJSONObject(i);
String name1 = food.getString("name");
if (name.equals(name1)) {
imageUrl[0] = imageUrl[0] + i;
nametext.setText("Name : " + name);
String description = food.getString("description");
detailstext.setText("Description : " + description);
String rating = food.getString("rating");
String price = food.getString("price");
price1 = Integer.parseInt(price);
pricetext.setText("Price : Rs. " + price);
ratingtext.setText("Rating : " + rating + " stars");
String cooktime = food.getString("cooktime");
cooktimetext.setText("Cooktime : " + cooktime);
JSONArray ingredients = food.getJSONArray("ingredients");
String[] ing = new String[ingredients.length()];
for (int k = 0; k < ingredients.length(); k++) {
JSONObject ingd = ingredients.getJSONObject(k);
ing[k] = ingd.getString("ingredient");
}
String ingre = "Ingredients:";
for (int k = 0; k < ing.length; k++) {
if (k < (ing.length - 1))
ingre = ingre + " " + ing[k] + ",";
else
ingre = ingre + " " + ing[k];
}
ingredientstext.setText(ingre);
break;
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
我需要帮助.. 这是我从服务器获取的JSON:
[
{
“不可用”:
[
{
“成分”:“虾”
},
{
"ingredient" : "Paneerygf"
},
{
"ingredient" : "Fish1"
}
]
},
{
"name" : "Paneer Chilly",
"description" : "Fried paneer pieces in chilly gravy",
"rating" : "4",
"price" : "100",
"cooktime" : "20 mins",
"ingredients" : [
{"ingredient":"Paneer"}
]
},
{
"name" : "Prawn Stuff Papad",
"description" : "Papad stuffed with prawns and spices",
"rating" : "3",
"price" : "150",
"cooktime" : "25 mins",
"ingredients" : [
{"ingredient":"Prawn"}
]
},
{
"name" : "Fish Chilly",
"description" : "Fired fish fillets with chilly gravy",
"rating" : "3",
"price" : "175",
"cooktime" : "25 mins",
"ingredients" : [
{"ingredient":"Fish"}
]
}
答案 0 :(得分:0)
http://mahmoudfa-001-site1.atempurl.com/appetizers.json返回的响应是JSONArray,而不是JSONObject。您应该按JSONArray jsonArray = new JSONArray(responseString)
解析它。