android怎么根据标题解析json,如何在android中解析没有json对象标题的json数组?

首先,我创建了解析器类JSONParser.java

package com.example.myparse;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.StatusLine;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.HttpClient;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.json.JSONArray;

import org.json.JSONException;

import org.json.JSONObject;

import android.util.Log;

public class JSONParser {

static InputStream is = null;

static JSONArray jarray = null;

static String json = "";

// constructor

public JSONParser() {

}

public JSONArray getJSONFromUrl(String url) {

StringBuilder builder = new StringBuilder();

HttpClient client = new DefaultHttpClient();

HttpGet httpGet = new HttpGet(url);

try {

HttpResponse response = client.execute(httpGet);

StatusLine statusLine = response.getStatusLine();

int statusCode = statusLine.getStatusCode();

if (statusCode == 200) {

HttpEntity entity = response.getEntity();

InputStream content = entity.getContent();

BufferedReader reader = new BufferedReader(new InputStreamReader(content));

String line;

while ((line = reader.readLine()) != null) {

builder.append(line);

}

} else {

Log.e("==>", "Failed to download file");

}

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

// try parse the string to a JSON object

try {

jarray = new JSONArray( builder.toString());

//System.out.println(""+jarray);

} catch (JSONException e) {

Log.e("JSON Parser", "Error parsing data " + e.toString());

}

// return JSON String

return jarray;

}

}然后像这样在主类中创建一个对象....

ArrayList> contactList = new ArrayList>(); // used to display in list view

JSONParser jParser = new JSONParser();

JSONArray json = jParser.getJSONFromUrl(url); // pass your ulr here

for(int i = 0; i <= json.length(); i++) // using for loop for parsing

{

try

{

JSONObject c = json.getJSONObject(i);

String name = c.getString(TAG_NAME);

String imagename = c.getString(TAG_IMAGENAME);

String active = c.getString(TAG_ACTIVE);

String createdon = c.getString(TAG_CREATEDON);

String lastmodifiedon = c.getString(TAG_LASTMODIFIEDON);

String description = c.getString(TAG_DESCRIPTION);

String id = c.getString(TAG_ID);

// If you want to show your parsed value in list view add the values into the array list

// creating new HashMap

HashMap map = new HashMap();

// adding each child node to HashMap key => value

map.put(TAG_NAME, name);

map.put(TAG_IMAGENAME, imagename);

map.put(TAG_ACTIVE, active);

map.put(TAG_CREATEDON, createdon);

map.put(TAG_LASTMODIFIEDON, lastmodifiedon);

map.put(TAG_DESCRIPTION, description);

map.put(TAG_ID, id);

// adding HashList to ArrayList

contactList.add(map);

System.out.println("contactlist---->"+contactList);

}

catch (JSONException e)

{

e.printStackTrace();

}

}我是这样做的。我得到了正确的输出。祝一切顺利。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值