0

这是我想在我的应用程序中解析的 JSON 数据:

{
  "viewdeal":[
  {
     "viewdeal1":{
        "teetimeId":"33",
        "golfcourseId":"127",
        "offers":"descount of $2",
        "time":"8\/7\/pm",
        "amount":"90",
        "month":"august",
        "year":"2012",
        "date":"21",
        "day":"monday",
        "created_date":"0",
        "golfcourse_name":"SilverHorn Golf Club"
     },
     "msz":"Book"
   }
 ]
}

我用来解析 JSON 数据的代码是:

JsonParser jParser = new JsonParser();
JSONObject json = jParser.getJSONfromUrl("http://mygogolfteetime.com/iphone/viewdeal/127");
JSONArray viewdeal = json.getJSONArray(TAG_VWDL);
                vwdlsList.clear();
                for(int i=0; i<viewdeal.length(); i++)
                {
                    JSONObject v1 = viewdeal.getJSONObject(i);
                    msz = v1.getString(TAG_MSZ);
                    viewdeal1 = v1.getString(TAG_VWDL1);
                    JSONArray viewdeal1 = json.getJSONArray(TAG_VWDL1); 
                    for(int j=0; j<viewdeal1.length(); j++)
                    {
                        JSONObject v2 = viewdeal.getJSONObject(j);
                        teetimeId = v2.getString(TAG_TTIMEID);
                        golfcourseId = v2.getString(TAG_GLFCRSID);
                        offers = v2.getString(TAG_OFR);
                        time = v2.getString(TAG_TIME);
                        amount = v2.getString(TAG_AMNT);
                        month = v2.getString(TAG_MNTH);
                        year = v2.getString(TAG_YEAR);
                        date = v2.getString(TAG_DATE);
                        day = v2.getString(TAG_DAY);
                        created_date = v2.getString(TAG_CRTDATE);
                        golfcourse_name = v2.getString(TAG_GLFCRSNAME);
                    }

                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(TAG_TTIMEID, teetimeId);
                    map.put(TAG_GLFCRSID, golfcourseId);
                    map.put(TAG_MSZ, msz);
                    map.put(TAG_OFR, offers);
                    map.put(TAG_TIME, time);
                    map.put(TAG_AMNT, amount);
                    map.put(TAG_MNTH, month);
                    map.put(TAG_YEAR, year);
                    map.put(TAG_DATE, date);
                    map.put(TAG_DAY, day);
                    map.put(TAG_CRTDATE, created_date);
                    map.put(TAG_GLFCRSNAME, golfcourse_name);

                    vwdlsList.add(map);
                }
            }

在上面的代码中,我能够解析 msz 和 viewdeal1 的值,但是当我尝试解析 viewdeal1 中的剩余值时,我在 LogCat 中收到以下消息:

08-22 12:03:46.034: V/GoGolf(1178): org.json.JSONException: No value for viewdeal1

如何解析 viewdeal1 中的剩余值。

提前致谢..

4

4 回答 4

1

尝试这个...

nParser jParser = new JsonParser();
JSONObject json = jParser.getJSONfromUrl("http://mygogolfteetime.com/iphone/viewdeal/127");
JSONArray viewdeal = json.getJSONArray(TAG_VWDL);
                vwdlsList.clear();
                for(int i=0; i<viewdeal.length(); i++)
                {

             ---->> **JSONObject v1= viewdeal.getJSONObject(i).getJSONObject("viewdeal1");**

                         .....
                         .....      
                }
于 2012-08-22T07:12:24.190 回答
0

您两次要求“viewdeal1”键:

     viewdeal1 = v1.getString(TAG_VWDL1);
     JSONArray viewdeal1 = json.getJSONArray(TAG_VWDL1);

第二行是正确的,您希望将其作为对象,应删除第一个列表。

     JSONObject viewdeal1 = json.getJSONObject(TAG_VWDL1);

如果您检查 LogCat,它会告诉您确切的行:

高尔夫(1178)

这是 GoGolf.java 第 1178行,在 eclipse 中打开行号,我猜第 1178 行是viewdeal1 = v1.getString(TAG_VWDL1);因为No value for viewdeal1它找不到String与该键匹配的,因为它是JSONObject

于 2012-08-22T06:58:42.553 回答
0

嗨,这viewdeal1不是array一个object

改变

JSONArray viewdeal1 = json.getJSONArray(TAG_VWDL1); 

JSONObject viewdeal1 = json.getJSONObject(TAG_VWDL1);

并删除

viewdeal1 = v1.getString(TAG_VWDL1);

array并请删除 for 循环,因为没有viewdeal1

ajson array'['括号开头

于 2012-08-22T07:01:43.757 回答
0

尝试这个

JsonParser jParser = new JsonParser();
JSONObject json = jParser.getJSONfromUrl("http://mygogolfteetime.com/iphone/viewdeal/127");
 JSONArray viewdeal = json.getJSONArray(TAG_VWDL);
            vwdlsList.clear();

for(int i=0; i<</>viewdeal.length(); i++)
          {
                JSONObject v1 = viewdeal.getJSONObject(i);
                msz = v1.getString(TAG_MSZ);
JSONObject v2 = v1.getJSONObject(TAG_VWDL1);
 teetimeId = v2.getString(TAG_TTIMEID);
                    golfcourseId = v2.getString(TAG_GLFCRSID);
                    offers = v2.getString(TAG_OFR);
                    time = v2.getString(TAG_TIME);
                    amount = v2.getString(TAG_AMNT);
                    month = v2.getString(TAG_MNTH);
                    year = v2.getString(TAG_YEAR);
                    date = v2.getString(TAG_DATE);
                    day = v2.getString(TAG_DAY);
                    created_date = v2.getString(TAG_CRTDATE);
                    golfcourse_name = v2.getString(TAG_GLFCRSNAME);
 }

                HashMap<String, String> map = new HashMap<String, String>();
                map.put(TAG_TTIMEID, teetimeId);
                map.put(TAG_GLFCRSID, golfcourseId);
                map.put(TAG_MSZ, msz);
                map.put(TAG_OFR, offers);
                map.put(TAG_TIME, time);
                map.put(TAG_AMNT, amount);
                map.put(TAG_MNTH, month);
                map.put(TAG_YEAR, year);
                map.put(TAG_DATE, date);
                map.put(TAG_DAY, day);
                map.put(TAG_CRTDATE, created_date);
                map.put(TAG_GLFCRSNAME, golfcourse_name);

                vwdlsList.add(map);
            }
        }
于 2012-08-22T07:25:09.190 回答