如何在 Android 中解析JSON 网址?
例如:
我单击一个按钮,从服务器下载数据,然后在屏幕上显示数据。
JSON代码:
[{"id":1, "name":"ABC"},{"id":2,"name":"ABC"}]
OR
{"response":[{"id":1,"name":"ABC","surname":"ABC"}]}
答案 0 :(得分:1)
要从url获取数据,您可以使用android-async-http-1.4.1.jar(可以从http://loopj.com/android-async-http/下载), 并将其放在libs文件夹中,然后使用以下代码,如果json结构如此“{”响应“:[{”id“:1,”name“:”ABC“,”surname“:”ABC“}]} “:
AsyncHttpClient client = new AsyncHttpClient();
client.post(Your_URL_String, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String MyResponse) {
JSONObject json_main;
try {
JSONObject json_main = new JSONObject(MyResponse);
JSONArray json_arr = json_main.getJSONArray("response");
for(int i = 0; i < json_arr.length(); i++)
{
JSONObject c = json_arr.getJSONObject(i);
String ID = c.getString("id");
String name = c.getString("name");
String surname = c.getString("surname");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@Override
public void onFailure(Throwable arg0, String arg1) {
// TODO Auto-generated method stub
super.onFailure(arg0, arg1);
}
});
答案 1 :(得分:1)
private void Get_List() {
// TODO Auto-generated method stub
CountDownTimer count_Timer;
try {
count_Timer.start();
SetUrl url = new SetUrl();
URL myurl = new URL(//"Your URL");
HttpURLConnection connection = (HttpURLConnection) myurl
.openConnection();
connection.connect();
int code = connection.getResponseCode();
if (code != 200) {
setContentView(R.layout.webimg);
TextView tv_nointernet = (TextView) findViewById(R.id.tv_nointernet);
tv_nointernet.setText("Please turn your internet on. . .");
} else {
BufferedReader dis = new BufferedReader(
new InputStreamReader(connection.getInputStream()));
String myString, Result = dis.readLine();
while ((myString = dis.readLine()) != null) {
Result += myString;
}
JSONObject jsonObject = new JSONObject(Result);
JSONArray jsonArray = jsonObject.getJSONArray("posts");
Rest_List = new ArrayList<Rest_Listing>();
for (int i = 0; i < jsonArray.length(); i++) {
Rest_Listing R_List = new Rest_Listing();
JSONObject O = jsonArray.getJSONObject(i);
R_List.setRest_Address1(O.getJSONObject("post")
.getString("Name"));
Rest_List.add(i, R_List);
}
count_Timer.cancel();
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
答案 2 :(得分:0)
Use the following it will be helpful
public void JSONPARSE()
{
JSONObject jObj=null;
JSONArray Jarray=null;
InputStream is=null;
String URL ="url";
System.out.println("verify url is----> "+URL);
//Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(URL);
HttpResponse httpResponse = httpClient.execute(httpPost);
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();
String result = sb.toString();
System.out.println("json RegistrationResult is----> "+result);
try {
jObj = new JSONObject(result);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
try {
Jarray = jObj.getJSONArray("response");
for(int i = 0; i < Jarray.length(); i++)
{
JSONObject c = Jarray.getJSONObject(i);
String id = c.getString("id");
x.add(id); //x is a vector
String name =c.getString("name");
n.add(name); //n is a vector
String surname =c.getString("surname");
s.add(surname); //s is a vector
}
} catch (JSONException e) {
e.printStackTrace();
}
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
}
答案 3 :(得分:0)
有很多方法可以做到这一点。 我发现这个库非常有用且省时:Gson Library。
您可以在此处访问用户指南: Gson User Guide