-1

I want to this kind of Json Parsing which is provide in below and fetch the data from Json Parsing Data. But it give me NullPointer Exception whenever Execute my code(Do in Background Process) Which is provided in below. How to solve this problem? How to get the JSON Response from this below URL?

data = {"2015-03-06":[{"date":"2015-03-06","sign":"0"}]};

My Code is,

 @Override
     urlGetData= "view-source:XXXXXXXX?date=YYYY-MM-DD&sign=2XXXX";
            protected Void doInBackground(Void... arg0) {
                        dataList = new ArrayList<HashMap<String, String>>();
                        // Retrieve JSON Objects from the given URL address
                        jsonobject = JSONFunctions.getJSONfromURL(urlGetData);
                        try {


                            jsonarray = jsonobject.getJSONArray("YYYY-MM-DD");

                            for (int i = 0; i < jsonarray.length(); i++) {

                                HashMap<String, String> map = new HashMap<String, String>();
                                jsonobject = jsonarray.getJSONObject(i);
                                // Retrive JSON Objects

                                strRating = String.valueOf(jsonobject
                                        .getString("rating"));
                                dataList.add(map);
                            }
                        } catch (JSONException e) {
                            Log.e("Error", e.getMessage());
                            e.printStackTrace();
                        }

                        return null;
                    }

But it gives me Null Pointer Exception on jsonarray = jsonobject.getJSONArray("YYYY-MM-DD").

How can get theresponse of data = {"2015-03-06":[{"date":"2015-03-06","sign":"0"}]};?

Thanks.

Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
Reena
  • 563
  • 4
  • 11
  • 22

3 Answers3

0

try using

jsonarray = jsonobject.getJSONArray("2015-03-06");

instead of

jsonarray = jsonobject.getJSONArray("YYYY-MM-DD");

because jsonArray's key is "2015-03-06" not "YYYY-MM-DD".Hope it will help.

Nirav Tukadiya
  • 3,367
  • 1
  • 18
  • 36
  • I am also trying this kind of data but It Not convert into Valid Json Parsing which Provide in URL data = {"2015-03-06":[{"date":"2015-03-06","sign":"0"}]}; – Reena Mar 04 '15 at 10:27
0

if it is always correct date then you can parse it as below:-

DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date date = new Date();
jsonarray = jsonobject.getJSONArray(dateFormat.format(date).toString());

then get the values you want from this array,but i will still say having date as key in JSON response is a very bad idea.

Pramod Yadav
  • 2,316
  • 2
  • 23
  • 36
0

Here you are using "YYYY-MM-DD". Which is not going to return the current date. It is always "YYYY-MM-DD". So You are try to read json with the key name "YYYY-MM-DD". That is the reason you are getting the null pointer.

If this is the case then you can get Current date and time by using other functions For ex.

Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String strDate = sdf.format(c.getTime());

And the next thing you can pass this date as your key.

ex. jsonarray = jsonobject.getJSONArray(strDate);

Amsheer
  • 7,046
  • 8
  • 47
  • 81