用jsonArray解析数据例子

前端之家收集整理的这篇文章主要介绍了用jsonArray解析数据例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

1:需要解析的数据格式为:

{"results": {
"ju_response": "success",
"get_mark_list": [
{
"markId": 1,
"ID": "111",
"goods_id": "1",
"markContent": "akaja",
"userNickname": "jianjian",
"markTime": "2011-05-20 17:40:17.0",
"markStar": 11.0
},
{
"markId": 2,
"replyId": 1,
"ID": "222",
"markContent": "jajjajaja",
"userNickname": "dongdong",
"markStar": 2.0
},
{
"markId": 3,
"ID": "333",
"markContent": "hashen",
"userNickname": "taotao",
{
"markId": 4,
"ID": "44",
"markContent": "agwioagwenoijjgoaiejfwjag",
"userNickname": "hello",
"markTime": "2011-05-24 18:01:09.0",
"markStar": 3.0
},
{
"markId": 5,
"ID": "55",
"markTime": "2011-05-24 18:02:16.0",
"markStar": 3.0
}
]
}}
2:解析数据的程序为:

private void getPDAServerData(String url) {
HttpClient client = new DefaultHttpClient();
//提拱默认的HttpClient实现
HttpPost request;
String showMessage = "";
try {
request = new HttpPost(new URI(url));
HttpResponse response = client.execute(request);
// 判断请求是否成功
if (response.getStatusLine().getStatusCode() == 200) { //200表示请求成功
HttpEntity entity = response.getEntity();
if (entity != null) {
String out = EntityUtils.toString(entity);
JSONArray marks = null;
try {
JSONObject obj = new JSONObject(out).getJSONObject("results");
String ju_response = obj.getString("ju_response");

if("success".equals(ju_response))
{
marks =obj.getJSONArray("get_mark_list");
}

for(int i=0; i< marks.length(); i++){
JSONObject mark = (JSONObject)marks.get(i);
showMessage += mark.getString("markId");

String replyId = mark.getString("replyId");
if(replyId != null)showMessage+=","+replyId;
showMessage += ",";
showMessage += mark.getString("ID");
showMessage += "; ";

}

} catch (JSONException e) {e.printStackTrace();} new AlertDialog.Builder(this).setMessage(showMessage).create().show(); } } } catch (URISyntaxException e) { e.printStackTrace(); new AlertDialog.Builder(this).setMessage(e.getMessage()).create() .show(); } catch (ClientProtocolException e) { e.printStackTrace(); new AlertDialog.Builder(this).setMessage(e.getMessage()).create() .show(); } catch (IOException e) { e.printStackTrace(); new AlertDialog.Builder(this).setMessage(e.getMessage()).create() .show(); } }

原文链接:https://www.f2er.com/json/290773.html

猜你在找的Json相关文章