需要帮助解析Android中的这个json。 解析必须通过URL获取。 http://www.azmetr.com/view.json 得到了很多教程。但无法学习如何解析这种基础。
我使用了原生的JSONObject。或者更好地使用GSON或其他人?
{
"azmetr":[
{
"mebel":{
"nid":"39",
"title":"Oturacaq 4"
}
},
{
"mebel":{
"nid":"38",
"title":"Oturacaq 3"
}
},
{
"mebel":{
"nid":"37",
"title":"Oturacaq 2"
}
},
{
"mebel":{
"nid":"36",
"title":"Oturacaq 1"
}
},
{
"mebel":{
"nid":"35",
"title":"Ag kreslo"
}
},
{
"mebel":{
"nid":"34",
"title":"Ag divan"
}
},
{
"mebel":{
"nid":"33",
"title":"Zebra kreslo"
}
},
{
"mebel":{
"nid":"32",
"title":"Zebra divan"
}
},
{
"mebel":{
"nid":"31",
"title":"Korolevski kreslo"
}
},
{
"mebel":{
"nid":"30",
"title":"Korolevski divan"
}
}
]
}
答案 0 :(得分:0)
尝试使用此代码可以解析json响应数组。这段代码对我有用。
public void update(){
try {
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 0);
HttpConnectionParams.setSoTimeout(httpParams, 0);
//
HttpParams p = new BasicHttpParams();
p.setParameter("user", "1");
// Instantiate an HttpClient
HttpClient httpclient = new DefaultHttpClient(p);
String url = "Your URL";
Log.i(url, "send task - start webservice");
HttpPost httppost = new HttpPost(url);
// Instantiate a GET HTTP method
try {
Log.i(getClass().getSimpleName(), "send task - start");
//
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = httpclient.execute(httppost, responseHandler);
// Parse
JSONObject json = new JSONObject(responseBody);
JSONArray jArray = json.getJSONArray("posts");
int count = jArray.length();
mebel= new String[count];
nid= new String[count];
title = new String[count];
for (int i = 0; i < jArray.length(); i++) {
JSONObject e = jArray.getJSONObject(i);
String s = e.getString("post");
JSONObject jObject = new JSONObject(s);
mebel[i] = jObject.getString("mebel");
nid[i] = jObject.getString("nid");
title[i] = jObject.getString("title");
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (Throwable t) {
Toast.makeText(HomeActivity.this, "Request failed: " + t.toString(),Toast.LENGTH_LONG).show();
}
}
注意:如果您使用的是2.3版本或api级别10,则需要使用此代码并将其放在onCreate之后。
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
答案 1 :(得分:0)
嘿,没有人可以在SO All the people is ready to solving problem not teaching
教授。
请参阅this示例,如果您遇到任何问题,请先了解自己,然后发表评论。