0

this the JSON data that i want to parse in my application:

{
  "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"
   }
 ]
}

The code that i am using to parse the JSON data is:

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

In the above code i am able to parse the value of msz and viewdeal1 but when i try to parse the remaining values that are in the viewdeal1 i receive the following message in my LogCat:

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

How can I parse the remaining values that are in the viewdeal1..

Thanks in advance..

4
  • This may helps you. Commented Aug 22, 2012 at 6:49
  • Didn't take a look at that example? Commented Aug 22, 2012 at 6:59
  • This one Commented Aug 22, 2012 at 7:15
  • sir I have followed this tutorial and when i change the code to this one: JSONObject viewdeal1 = viewdeal.getJSONObject(TAG_VWDL1); it gives following errors: "change the type of TAG_VWDL1 to int" and "change getJSONObject(..) to optJSONObject(..)" and "change getJSONObject(..) to toJSONObject(..)" .... How to over come these errors? Commented Aug 22, 2012 at 7:48

4 Answers 4

1

Try this...

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");**

                         .....
                         .....      
                }
Sign up to request clarification or add additional context in comments.

Comments

0

Your asking for the "viewdeal1" key twice:

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

The second line is correct you want it as an object, the first list should be deleted.

     JSONObject viewdeal1 = json.getJSONObject(TAG_VWDL1);

If you check your LogCat it tell's you the exact line:

GoGolf(1178)

which is GoGolf.java line 1178, turn on line numbers in your eclipse and I am guessing that line 1178 is viewdeal1 = v1.getString(TAG_VWDL1); is it saying No value for viewdeal1 because it can't find a String that matches that key because it is a JSONObject

Comments

0

try this

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

Comments

0

hi here viewdeal1 is not an array its an object

change

JSONArray viewdeal1 = json.getJSONArray(TAG_VWDL1); 

to

JSONObject viewdeal1 = json.getJSONObject(TAG_VWDL1);

and delete

viewdeal1 = v1.getString(TAG_VWDL1);

and please remove the for loop as there is no array in viewdeal1

a json array starts with '[' bracket

1 Comment

sir I have changed the JSONArray viewdeal1 = json.getJSONArray(TAG_VWDL1); to JSONObject viewdeal1 = json.getJSONObject(TAG_VWDL1); and also deleted viewdeal1 = v1.getString(TAG_VWDL1); But still i am getting the same error in my LogCat.. can you please be more specific as I am new to JSON parsing..

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.