打印JSON到logcat无法正常工作

时间:2013-07-02 20:39:23

标签: android json eclipse eclipse-adt

我一直在向我的logcat打印JSON,所以我可以看到它正在工作,它会停止打印JSON字符串。有人能帮助我解决问题吗?我还需要遍历JSON以找到一个特定的字符串,如何做到这一点?

LOGCAT SNIPET :(它很长,所以我只是添加了结尾部分)

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 org.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)

这是Class snipet:

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();
    }

提前非常感谢你!

2 个答案:

答案 0 :(得分:0)

您必须使用locations.toString(1),其中1是制表。

使用:Log.d("TAG_LOCATIONS", locations.toString(1));

答案 1 :(得分:0)

在你的logcat中有一行说,

  

07-02 20:08:19.888:W / System.err(4597):org.json.JSONException:没有id的值

我强烈怀疑这行代码会导致异常。

  

//将每个json项存储在变量

中      

String id = c.getString(TAG_ID);

JSONObject.getString(String)抛出一个JSONException“如果该键没有字符串值”。所以这意味着在你的JSONObject中,键id没有字符串值。因此,请确保您的JSON数据包含密钥id

变通方法

如果没有使用optString(java.lang.String key, java.lang.String defaultValue)的密钥值,您可以选择设置默认值,“获取与密钥相关联的可选字符串。”,或者在直接访问之前使用has(java.lang.String key)进行检查到具体的关键。