1

I am new to Android/ Java programming and need help in knowing how to convert json map to a java hashmap. Please refer the code below:

HttpPost httpPost1 = new HttpPost(
                        "http://www.mywebsite.com/something.json" );
                Log.i("MAP", "Hash call");
                List<NameValuePair> nameValuePair1 = new ArrayList<NameValuePair>(1);
                nameValuePair1.add(new BasicNameValuePair("id_token", token ));

                    try {
                        httpPost1.setEntity(new UrlEncodedFormEntity(nameValuePair1));
                    } catch (UnsupportedEncodingException e) {
                        // writing error to Log
                        e.printStackTrace();
                    }
         try {
                    HttpResponse response1 = httpClient.execute(httpPost1);
                    BufferedReader reader = new            BufferedReader(newInputStreamReader(response1.getEntity().getContent(), "UTF-8"));
                   StringBuilder builder = new StringBuilder();
                   for (line = null; (line = reader.readLine()) != null;) {
                      // builder.append(line).append("\n");
                        System.out.println(line);  }

This code returns me a jason map, which has been shown below:

{"myemailid@gmail.com":[{"lat":0.0,"lng":0.0,"tim":"2014-10-01T16:55:15.002Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-01T16:18:07.290Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-01T12:04:06.364Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-01T11:58:04.455Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-01T11:46:24.560Z"}]}

What I need is to convert this json map to java hashmap. I would highly appreciate if I could be guided on this.

dimo414
  • 47,227
  • 18
  • 148
  • 244
user1903022
  • 1,075
  • 1
  • 13
  • 19
  • Thanks a lot for the response Antoine, however, I tried the solution given on the link posted by you, but, have been getting stackoverflow exception. I think I am missing something while trying to convert json to java hashmap. Could you help me by writing a small code with respect to the json that I have posted in my code. It would be of great help to me. – user1903022 Oct 01 '14 at 18:43
  • StackOverflowError means some call is recursive. You should look where the code is calling itself back producing an infinite loop. – Antoine Marques Oct 01 '14 at 18:45
  • If you're getting an error, post a question about the error (e.g. "Getting a StackOverflowError while using Gson") and include the exact error message and stack trace, along with the relevant code. – dimo414 Oct 02 '14 at 04:15

0 Answers0