Please do not mark it as duplicate, I went through many of those related posts, but in vain. 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();
}
// Making HTTP Request
try {
HttpResponse response1 = httpClient.execute(httpPost1);
BufferedReader reader = new BufferedReader(new InputStreamReader(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);
Log.i("Line1", "line1 "+ line);
class Data {
private Email email;
private String result;
// Add/generate getters and setters.
class Email {
private List<Alert> alerts;
private String session;
// Add/generate getters and setters.
}
class Alert {
private Long AlertID;
private String tim;
private String lng;
private String lat;
}
}
Gson gson=new Gson();
String json="{\"AlertID\":\"tim\",\"lng\",\"lat\"}";
Map<String,String> map=new HashMap<String,String>();
java.lang.reflect.Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> myMap = gson.fromJson("{\"AlertID\":\"tim\",\"lng\",\"lat\"}", type);
System.out.println(json);
Log.i("JAVA Hash", "Java Hash call");
}//Closing for loop//line
JSONTokener tokener = new JSONTokener(builder.toString());
JSONArray finalResult = new JSONArray(tokener);
// writing response to log
Log.i("Http Response:", response1.toString());
Log.i("JSON returns: ", "JSON returns");
} catch (ClientProtocolException e) {
// writing exception to log
e.printStackTrace();
} catch (IOException e) {
// writing exception to log
e.printStackTrace();
}
The json which needs to be converted to hashmap is as follows:
{\"myemailid@gmail.com\":\"lat\":0.0,\"lng\":0.0,\"tim\":"2014-10-02T10:38:11.437Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-02T08:56:38.459Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-02T08:47:18.713Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-02T08:43:58.145Z"},{"lat":0.0,"lng":0.0,"tim":"2014-10-02T08:34:08.798Z"}]}
Just to make the json more readable:
myemailid@gmail.com
lat : 0 lng : 0 tim : "2014-10-01T16:55:15.002Z"
lat : 0 lng : 0 tim : "2014-10-01T16:18:07.290Z"
lat : 0 lng : 0 tim : "2014-10-01T12:04:06.364Z"
lat : 0 lng : 0 tim : "2014-10-01T11:58:04.455Z"
lat : 0 lng : 0 tim : "2014-10-01T11:46:24.560Z"
Any help in this regard would be highly appreciated.