可能重复:
如何在 Android 中解析这个 JSON?
我正在关注此链接来解析我的 json,但我无法执行,因为我很困惑。甚至重复的链接也没有提供任何参考,那些投票关闭这个问题的用户可以在他们提供的链接没有提供任何帮助来解决这些问题时回答我的问题吗?
我认为那些无法回答问题的人无权编辑某人的问题,也无权在他们提供的链接无法解决问题时关闭问题。帮我解析数据。
[
{
"id": "c200",
"name": "XYZ",
"email": "xyz@gmail.com",
},
{
"id": "c201",
"name": "Johnny",
"email": "john_johnny@gmail.com",
}]
[回答]
由于这是一个封闭的帖子,所以我发布了我的答案:
//JSONArray
JSONArray hospital = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
String json = jParser.makeHttpRequest(url, "GET",
contactList);
try {
// Getting Array of Contacts
hospital = new JSONArray(json);
if (hospital != null) {
// looping through
for (int i = 0; i < hospital.length(); i++) {
JSONObject contacts = hospital.getJSONObject(i);
// Storing each json item in variable
String id = contacts.getString(TAG_HID);
String name = contacts.getString(TAG_HNAME);
String address = contacts.getString(TAG_HADDRESS);
String phone1=contacts.getString(TAG_HPHONE1);
String phone2=contacts.getString(TAG_HPHONE2);
String fax=contacts.getString(TAG_HFAX);
String email=contacts.getString(TAG_HEMAIL);
String url=contacts.getString(TAG_URL);
String hlongitude=contacts.getString(TAG_LONGITUDE);
String hlatitude=contacts.getString(TAG_LATITUDE);
String hdistance=contacts.getString(TAG_HDISTANCE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_HID, id);
map.put(TAG_HNAME, name);
map.put(TAG_HADDRESS, address);
map.put(TAG_HPHONE1, phone1);
map.put(TAG_HPHONE2, phone2);
map.put(TAG_HFAX, fax);
map.put(TAG_HEMAIL, email);
map.put(TAG_URL, url);
map.put(TAG_LONGITUDE, hlongitude);
map.put(TAG_LATITUDE, hlatitude);
map.put(TAG_HDISTANCE, hdistance);
// adding HashList to ArrayList
contactList.add(map);
}
}
}
catch (JSONException e) {
e.printStackTrace();
}
JSON Parser 类的一些编码
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
// function get json from url
// by making HTTP POST or GET method
public String makeHttpRequest(String url, String method,
ArrayList<HashMap<String, String>> contactList) {
// Making HTTP request
try {
// check for request method
if (method == "POST") {
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity((List<? extends NameValuePair>) contactList));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
else if (method == "GET")
{
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format((List<? extends NameValuePair>) contactList, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (ClientProtocolException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// return JSON String
return json;
}
}