Android 循环遍历多个JSON数组
这是我的JSON的一个片段,它非常长,在“菜单”部分之后,它返回到“位置”,并从一个新的位置重新开始。我不知道为什么,但我在哪里循环并将其打印到我的logcat,它将在“菜单”部分的随机位置停止并抛出错误。我不知道如何进入整个菜单区Android 循环遍历多个JSON数组,android,eclipse,adt,eclipse-adt,Android,Eclipse,Adt,Eclipse Adt,这是我的JSON的一个片段,它非常长,在“菜单”部分之后,它返回到“位置”,并从一个新的位置重新开始。我不知道为什么,但我在哪里循环并将其打印到我的logcat,它将在“菜单”部分的随机位置停止并抛出错误。我不知道如何进入整个菜单区 { "locations": [{ "Location": { "id": "1", "name": "Scott Express Cafe", "building": "PKI", "mond
{
"locations": [{
"Location": {
"id": "1",
"name": "Scott Express Cafe",
"building": "PKI",
"monday_hours": "8am-2:30pm",
"tuesday_hours": "8am-2:30pm",
"wednesday_hours": "8am-2:30pm",
"thursday_hours": "8am-2:30pm",
"friday_hours": "8am-2:30pm",
"saturday_hours": "Closed",
"sunday_hours": "Closed"
},
"Section": [{
"id": "3",
"location_id": "1",
"name": "Snacks",
"Location": {
"id": "1",
"name": "Scott Express Cafe",
"building": "PKI",
"monday_hours": "8am-2:30pm",
"tuesday_hours": "8am-2:30pm",
"wednesday_hours": "8am-2:30pm",
"thursday_hours": "8am-2:30pm",
"friday_hours": "8am-2:30pm",
"saturday_hours": "Closed",
"sunday_hours": "Closed"
},
"Menu": [{
"id": "278",
"location_id": "1",
"item_name": "Pop-tarts",
"price_small": "$1.00",
"price_medium": "",
"price_large": "",
"special": false,
"special_start": "",
"special_end": "",
"details": "",
"section_id": "3"
}, {
"id": "279",
"location_id": "1",
"item_name": "Cereal Bar",
"price_small": "$1.00",
"price_medium": "",
"price_large": "",
"special": false,
"special_start": "",
"special_end": "",
"details": "",
"section_id": "3"
},
错误如下:
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): },
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): {
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "price_medium": "",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "id": "295",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "details": "",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "price_large": "",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "item_name": "Wheat Thins Toasted Chips",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "special_end": "",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "special_start": "",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "section_id": "3",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "special": false,
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "price_small": "$0.85",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "location_id": "1"
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): },
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): {
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "pric
07-02 20:08:19.888: W/System.err(4597): org.json.JSONException: No value for id
07-02 20:08:19.908: W/System.err(4597): at o rg.json.JSONObject.get(JSONObject.java:354)
07-02 20:08:19.908: W/System.err(4597): at org.json.JSONObject.getString(JSONObject.java:510)
07-02 20:08:19.908: W/System.err(4597): at com.example.androidtablayout.FoodMenuList.onCreate(FoodMenuList.java:87)
07-02 20:08:19.908: W/System.err(4597): at android.app.Activity.performCreate(Activity.java:5104)
07-02 20:08:19.918: W/System.err(4597): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-02 20:08:19.918: W/System.err(4597): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-02 20:08:19.918: W/System.err(4597): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-02 20:08:19.918: W/System.err(4597): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-02 20:08:19.918: W/System.err(4597): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-02 20:08:19.928: W/System.err(4597): at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 20:08:19.939: W/System.err(4597): at android.os.Looper.loop(Looper.java:137)
07-02 20:08:19.939: W/System.err(4597): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-02 20:08:19.948: W/System.err(4597): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 20:08:19.948: W/System.err(4597): at java.lang.reflect.Method.invoke(Method.java:511)
07-02 20:08:19.958: W/System.err(4597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-02 20:08:19.958: W/System.err(4597): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-02 20:08:19.958: W/System.err(4597): at dalvik.system.NativeStart.main(Native Method)
public class FoodMenuList extends Activity {
// url to make request for menus
private static String url = "http://mavmate.atticdev.ist.unomaha.edu/api/v1/services/getMenus";
// JSON Node names
private static final String TAG_LOCATIONS = "locations";
private static final String TAG_LOCATION = "Location";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_BUILDING = "building";
// contacts JSONArray
JSONArray locations = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foodmenulist_layout);
Bundle extras = getIntent().getExtras();
String locationIDThing = extras.getString("locationID");
// Hashmap for textView
ArrayList<HashMap<String, String>> menuItemList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of locations
locations = json.getJSONArray(TAG_LOCATIONS);
// looping through All locations
for(int i = 0; i < locations.length(); i++){
JSONObject c = locations.getJSONObject(i);
Log.d("TAG_LOCATIONS", locations.toString(i));
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String Location = c.getString(TAG_BUILDING);
String building = c.getString(TAG_LOCATION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_LOCATION, Location);
map.put(TAG_BUILDING, building);
//map.put(TAG_PHONE_MOBILE, mobile);
// adding HashList to ArrayList
menuItemList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
我的班级:
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): },
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): {
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "price_medium": "",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "id": "295",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "details": "",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "price_large": "",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "item_name": "Wheat Thins Toasted Chips",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "special_end": "",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "special_start": "",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "section_id": "3",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "special": false,
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "price_small": "$0.85",
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "location_id": "1"
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): },
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): {
07-02 20:08:19.878: D/TAG_LOCATIONS(4597): "pric
07-02 20:08:19.888: W/System.err(4597): org.json.JSONException: No value for id
07-02 20:08:19.908: W/System.err(4597): at o rg.json.JSONObject.get(JSONObject.java:354)
07-02 20:08:19.908: W/System.err(4597): at org.json.JSONObject.getString(JSONObject.java:510)
07-02 20:08:19.908: W/System.err(4597): at com.example.androidtablayout.FoodMenuList.onCreate(FoodMenuList.java:87)
07-02 20:08:19.908: W/System.err(4597): at android.app.Activity.performCreate(Activity.java:5104)
07-02 20:08:19.918: W/System.err(4597): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-02 20:08:19.918: W/System.err(4597): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-02 20:08:19.918: W/System.err(4597): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-02 20:08:19.918: W/System.err(4597): at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-02 20:08:19.918: W/System.err(4597): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-02 20:08:19.928: W/System.err(4597): at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 20:08:19.939: W/System.err(4597): at android.os.Looper.loop(Looper.java:137)
07-02 20:08:19.939: W/System.err(4597): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-02 20:08:19.948: W/System.err(4597): at java.lang.reflect.Method.invokeNative(Native Method)
07-02 20:08:19.948: W/System.err(4597): at java.lang.reflect.Method.invoke(Method.java:511)
07-02 20:08:19.958: W/System.err(4597): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-02 20:08:19.958: W/System.err(4597): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-02 20:08:19.958: W/System.err(4597): at dalvik.system.NativeStart.main(Native Method)
public class FoodMenuList extends Activity {
// url to make request for menus
private static String url = "http://mavmate.atticdev.ist.unomaha.edu/api/v1/services/getMenus";
// JSON Node names
private static final String TAG_LOCATIONS = "locations";
private static final String TAG_LOCATION = "Location";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_BUILDING = "building";
// contacts JSONArray
JSONArray locations = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foodmenulist_layout);
Bundle extras = getIntent().getExtras();
String locationIDThing = extras.getString("locationID");
// Hashmap for textView
ArrayList<HashMap<String, String>> menuItemList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of locations
locations = json.getJSONArray(TAG_LOCATIONS);
// looping through All locations
for(int i = 0; i < locations.length(); i++){
JSONObject c = locations.getJSONObject(i);
Log.d("TAG_LOCATIONS", locations.toString(i));
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String Location = c.getString(TAG_BUILDING);
String building = c.getString(TAG_LOCATION);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_LOCATION, Location);
map.put(TAG_BUILDING, building);
//map.put(TAG_PHONE_MOBILE, mobile);
// adding HashList to ArrayList
menuItemList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
公共类FoodMenuList扩展活动{
//用于请求菜单的url
专用静态字符串url=”http://mavmate.atticdev.ist.unomaha.edu/api/v1/services/getMenus";
//JSON节点名称
私有静态最终字符串标记_LOCATIONS=“LOCATIONS”;
私有静态最终字符串TAG_LOCATION=“LOCATION”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_NAME=“NAME”;
私有静态最终字符串TAG_BUILDING=“BUILDING”;
//联系JSONArray
JSONArray位置=空;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.foodmenulist\u布局);
Bundle extras=getIntent().getExtras();
String locationID=extras.getString(“locationID”);
//textView的Hashmap
ArrayList menuItemList=新建ArrayList();
//创建JSON解析器实例
JSONParser jParser=新的JSONParser();
//从URL获取JSON字符串
JSONObject json=jParser.getJSONFromUrl(url);
试一试{
//获取位置数组
locations=json.getJSONArray(标记位置);
//在所有位置循环
对于(int i=0;ivalue
地图放置(标签标识,标识);
地图放置(标签名称、名称);
地图放置(标签位置、位置);
地图放置(TAG_大楼,大楼);
//地图放置(标签、手机、手机);
//将哈希列表添加到ArrayList
menuItemList.add(地图);
}
}捕获(JSONException e){
e、 printStackTrace();
}
我认为您在JSON中的错误位置查找“id”
“id”出现在“位置”或“节”的元素中
但不是直接在“位置”元素中,这是代码正在查找的位置
编辑:
考虑使用“OpTxxx”函数代替“GETXXX”,例如OpString(“ID”)
< >,而不是手动解析JSON,以使用JSON对java对象进行反序列化来获得数据。例如,下面将将JSON字符串转换为HObjor对象。Person p = gson.fromJson(jsonStr, Person.class)
你能帮助我如何循环“菜单”部分吗?非常感谢,我将测试它:)这样你就知道你不能迭代JSON了