我跟着这个link来解析我的json,但我无法表现,因为我很困惑。甚至重复链接也没有提供任何参考,那些投票关闭此问题的用户可以在他们提供的LINK没有提供任何帮助来解决这些问题时回答我的问题?
我认为那些无法回答问题的人没有权利编辑某个人的问题,也没有权利在他们提供的链接没有提供任何解决方案时提出问题。 帮我解析数据。
[
{
"id": "c200",
"name": "XYZ",
"email": "xyz@gmail.com",
},
{
"id": "c201",
"name": "Johnny",
"email": "john_johnny@gmail.com",
}]
[ANSWER]
由于这是一个封闭的帖子所以我发布了我的答案:
//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;
}
}
答案 0 :(得分:0)
由于你没有说,我假设你正在使用Android附带的内置org.json解析器。如果您使用的是其他解析器,请参阅其文档。
将json作为字符串传递给JSONArray constructor。