-
Notifications
You must be signed in to change notification settings - Fork 1
/
JsonParser.java
67 lines (63 loc) · 2.16 KB
/
JsonParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.example.loginpage;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class JsonParser {
private HashMap<String,String> parseJasonObject(JSONObject object)
{
//Initialize hash map
HashMap<String,String> dataList = new HashMap<>();
try {
//get name from object;
String name = object.getString("name");
//get latitude from object
String latitude = object.getJSONObject("geometry")
.getJSONObject("location").getString("lat");
//get longitude
String longitude = object.getJSONObject("geometry")
.getJSONObject("location").getString("lng");
//Put all value in hash map
dataList.put("name",name);
dataList.put("lat",latitude);
dataList.put("lng",longitude);
} catch (JSONException e) {
e.printStackTrace();
}
//Return hash map
return dataList;
}
private List<HashMap<String,String>> parseJsonArray(JSONArray jsonArray)
{
//Initialize hash map list
List<HashMap<String,String>> dataList = new ArrayList<>();
for(int i=0;i<jsonArray.length();i++)
{
try {
//Initialize hash map
HashMap<String,String> data = parseJasonObject((JSONObject) jsonArray.get(i));
//Add data in hash map list
dataList.add(data);
} catch (JSONException e) {
e.printStackTrace();
}
}
//Return hash map list
return dataList;
}
public List<HashMap<String,String>> parseResult(JSONObject object)
{
//Innitialize jason array
JSONArray jsonArray = null;
//Get result array
try {
jsonArray = object.getJSONArray("results");
} catch (JSONException e) {
e.printStackTrace();
}
//Return array
return parseJsonArray(jsonArray);
}
}