Это ошибка: Значение [] в 0 типа org.json.JSONArray не может быть преобразовано в JSONObject. Я попытался передать параметр с использованием метода post, но его значение было приведено выше. JSON I am Trying is `JSon Parsing О Json Array
[
{
"menu_items": [
{
"menu_name": "Beverages",
"items": [
{
"id": 1,
"BaseName": "Coca-Cola",
"itemdesc": "",
"subitems": [
{
"id": 1,
"SubItemdesc": "0.33L",
"SubItemprice": "0.90"
},
{
"id": 2,
"SubItemdesc": "1.5L",
"SubItemprice": "2.00"
}
]
},
{
"id": 2,
"BaseName": "Diet Coca-Cola",
"itemdesc": "",
"subitems": [
{
"id": 1,
"SubItemdesc": "0.33L",
"SubItemprice": "0.90"
},
{
"id": 2,
"SubItemdesc": "1.5L",
"SubItemprice": "2.00"
}
]
},
{
"id": 3,
"BaseName": "Fanta",
"itemdesc": "",
"subitems": [
{
"id": 1,
"SubItemdesc": "0.33L",
"SubItemprice": "0.90"
}
]
},
{
"id": 4,
"BaseName": "Tango",
"itemdesc": "",
"subitems": [
{
"id": 1,
"SubItemdesc": "0.33L",
"SubItemprice": "0.90"
}
]
}
]
}'
Это код, который я пишу не в состоянии понять, у это вызывает как то
public class Secondlevel extends Activity {
ArrayList<JSONParser> itemsdata;
String item;
Block b=new Block();
Activity activity;
ListView sec;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.secondlist);
sec= (ListView) findViewById(R.id.seondlst);
Intent gettext=getIntent();
item=gettext.getStringExtra("name");
TextView test= (TextView) findViewById(R.id.textView);
test.setText(item);
itemsdata=new ArrayList<JSONParser>();
new loaditems().execute();
}
public class loaditems extends AsyncTask<String,String,String>{
@Override
protected String doInBackground(String... strings) {
String url="http://www.yell4food.com/json/data_standard_item_new.php?rname=standardtakeaway";
List<NameValuePair> params=new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("cname", item));
String plz=b.makeHttpRequest(url,"GET",params);
try {
JSONArray items=new JSONArray(plz);
for(int i=0;i<items.length();i++){
JSONObject citems=items.getJSONObject(i);
JSONArray menu_items=citems.getJSONArray("menu_items");
for (int j=0;j<menu_items.length();j++){
JSONObject sitems=menu_items.getJSONObject(j);
sitems.getString("menu_name");
JSONArray titems=sitems.getJSONArray("items");
for (int k=0;k<titems.length();k++){
JSONObject fitems=titems.getJSONObject(k);
JSONParser jsonParser=new JSONParser();
jsonParser.setBaseName(fitems.getString("BaseName"));
jsonParser.setItemdesc(fitems.getString("itemdesc"));
JSONArray fivitem=fitems.getJSONArray("subitems");
for(int l=0;l<fivitem.length();l++){
JSONObject subitems=fivitem.getJSONObject(l);
jsonParser.setSubItemdesc(subitems.getString("SubItemdesc"));
jsonParser.setSubItemprice(subitems.getString("SubItemprice"));
itemsdata.add(jsonParser);
}
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
runOnUiThread(new Runnable() {
@Override
public void run() {
Second_adapter secondAdapter=new Second_adapter(activity,itemsdata);
sec.setAdapter(secondAdapter);
}
});
}
}
}
Вы конвертируете JsonArray в JSONObject, поэтому ошибка 'JSONArray не может быть преобразована в JSONObject' –
я думаю, что я не делаю что .. –
'JSONObject citems = items.getJSONObject (я),' это должно быть JSONArray , потому что в соответствии с вашим json 'items' является массивом –