解析此JSON在Android中 [英] Parse this JSON in Android
问题描述
可能重复:结果
我如何在Android中解析此JSON?
块引用>
我下面这个链接解析的json,但我无法执行,因为我很困惑。甚至重复的链接,不提供任何参考,那些谁投赞成票,关闭这个问题可以回答我的问题时,他们提供的链接不提供任何帮助解决这些问题的用户?我认为那些谁也无法回答这个问题没有权利编辑一些人的问题,也没有权利关闭的问题时,他们提供的链接不提供任何角度解决这个问题。
帮我来解析数据。[
{
ID:C200,
名:XYZ,
电子邮件:xyz@gmail.com
},
{
ID:C201,
名:约翰尼
电子邮件:john_johnny@gmail.com}]【答案】
因为它是一个封闭的职位,所以我张贴我的问题的答案:
// JSONArray
JSONArray医院= NULL;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); //为哈希映射的ListView
ArrayList的<&HashMap的LT;字符串,字符串>> contactList =新的ArrayList<&HashMap的LT;字符串,字符串>>(); //创建JSON解析器实例
JSONParser jParser =新JSONParser(); // URL从获取JSON字符串
JSON字符串= jParser.makeHtt prequest(URL,GET,
联系人列表); 尝试{
//获取联系人的数组
医院=新JSONArray(JSON); 如果(医院!= NULL){ //通过循环
的for(int i = 0; I< hospital.length();我++){
JSONObject的接触= hospital.getJSONObject(I) //存储在变量中的每个JSON项目
字符串ID = contacts.getString(TAG_HID);
字符串名称= contacts.getString(TAG_HNAME);
字符串的地址= contacts.getString(TAG_HADDRESS);
字符串PHONE1 = contacts.getString(TAG_HPHONE1);
字符串PHONE2 = contacts.getString(TAG_HPHONE2);
串传真= contacts.getString(TAG_HFAX);
字符串email = contacts.getString(TAG_HEMAIL);
字符串URL = contacts.getString(TAG_URL);
字符串hlongitude = contacts.getString(TAG_LONGITUDE);
字符串hlatitude = contacts.getString(TAG_LATITUDE);
字符串hdistance = contacts.getString(TAG_HDISTANCE);
//创建新的HashMap
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>(); //将每个子节点的HashMap键=>值
map.put(TAG_HID,身份证);
map.put(TAG_HNAME,名);
map.put(TAG_HADDRESS,地址);
map.put(TAG_HPHONE1,PHONE1);
map.put(TAG_HPHONE2,PHONE2);
map.put(TAG_HFAX,传真);
map.put(TAG_HEMAIL,电子邮件);
map.put(TAG_URL,URL);
map.put(TAG_LONGITUDE,hlongitude);
map.put(TAG_LATITUDE,hlatitude);
map.put(TAG_HDISTANCE,hdistance); //添加HashList到ArrayList的
contactList.add(地图);
}
}
}
赶上(JSONException E){
e.printStackTrace();
}一些编码为JSON解析器类
公共类JSONParser {静态InputStream为= NULL;
静态的JSONObject jObj = NULL;
静态JSON字符串=;//构造
公共JSONParser(){}//函数得到JSON的网址
//通过HTTP POST或GET方法
公共字符串makeHtt prequest(URL字符串,字符串的方法,
ArrayList的<&HashMap的LT;字符串,字符串>>联系人列表) { //使HTTP请求
尝试{ //检查请求的方法
如果(方法==POST){
//请求方法是POST
// defaultHttpClient
DefaultHttpClient的HttpClient =新DefaultHttpClient();
HttpPost httpPost =新HttpPost(URL);
httpPost.setEntity(新UrlEn codedFormEntity((名单&;?延伸的NameValuePair&GT≤)contactList)); HTT presponse HTT presponse = httpClient.execute(httpPost);
HttpEntity httpEntity = HTT presponse.getEntity();
是= httpEntity.getContent(); }
否则,如果(方法==GET)
{
//请求方法是GET
DefaultHttpClient的HttpClient =新DefaultHttpClient();
字符串中的paramString = URLEn codedUtils.format((名单&LT ;?延伸的NameValuePair>)contactList,UTF-8);
网址+ =? +中的paramString;
HTTPGET HTTPGET =新HTTPGET(URL); HTT presponse HTT presponse = httpClient.execute(HTTPGET);
HttpEntity httpEntity = HTT presponse.getEntity();
是= httpEntity.getContent();
} }赶上(UnsupportedEncodingException五){
e.printStackTrace();
}
赶上(ClientProtocolException E){
e.printStackTrace();
}
赶上(IOException异常五){
e.printStackTrace();
} 尝试{
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(是,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\\ n);
}
is.close();
JSON = sb.toString();
}赶上(例外五){
Log.e(缓冲区错误,错误转换结果+ e.toString());
} //返回JSON字符串
返回JSON;}
}
解决方案既然你不说,我假设你使用内置的org.json解析器,Android的附带。如果您使用的是不同的解析器,请查阅其文档。
通过JSON作为一个字符串到<一个href=\"http://developer.android.com/reference/org/json/JSONArray.html#JSONArray%28java.lang.String%29\"相对=nofollow> JSONArray构造。
Possible Duplicate:
How can I parse this JSON in Android?I am following this link to parse my json but i am unable to perform as i am confused . and even the duplicate link does not provide any reference, those users who voted to close this question can answer my question when their provided LINK does not provide any help to solve these question?
i think those who can not answer the question have no rights to edit some one's question nor have rights to close the question when their provided link does not provide any point to solve this. Help me to parse data.
[ { "id": "c200", "name": "XYZ", "email": "xyz@gmail.com", }, { "id": "c201", "name": "Johnny", "email": "john_johnny@gmail.com", }]
[ANSWER]
Since it is an closed post so i am posting my answer in question:
//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(); }
some coding for JSON Parser class
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; } }
解决方案Since you don't say, I'm assuming you're using the built-in org.json parser that Android ships with. If you're using a different parser, consult its documentation.
Pass the json as a string to the JSONArray constructor.
这篇关于解析此JSON在Android中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!