【问题标题】:json reading issue array without title没有标题的json读取问题数组
【发布时间】:2015-06-11 01:06:25
【问题描述】:

我有一个没有标题的 JSON 数组。

我一直在尝试读取 JSON,但似乎一直失败。
我希望有某种不调用数组名即可解析 JSON 的示例。

任何帮助或指出我的示例方向将不胜感激。我将在下面附上我遇到错误的代码。这两个 url 是我试图读入的数据。

https://www.descartes.com/rest/glossary-items
https://www.descartes.com/rest/glossary-sources

JsonParser:

public class JsonParser {

    static InputStream is = null;
    static JSONArray jarray = null;
    static String json = "";

    public JSONArray getJSONFromUrl(String url1) {

        StringBuilder builder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url1);
        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;

    }
}

doInBackground:

protected Void doInBackground(Void... arg0) {
    String str = "";
    JsonParser sh = new JsonParser();

    // Making a request to url and getting response
    String jsonStr = sh.getJSONFromUrl(url1, JsonParser.GET);

    if (jsonStr != null){
        try{
            JSONArray jsonArr = new JSONArray(jsonArr);
            test = jsonArr.getJSONObje(str);
            // looping through All Contacts
            for (int i = 0; i <= str.length(); i++) {

                JSONObject c = str.getJSONObject(i);

                String tid = c.getString(TAG_TID);
                String title = c.getString(TAG_TITLE);
                String acronym = c.getString(TAG_ACRONYM);
                String description = c.getString(TAG_DESCRIPTION);


                // tmp hashmap for single contact
                HashMap<String, String> contact = new HashMap<String, String>();

                // adding each child node to HashMap key => value
                contact.put(TAG_TID, tid);
                contact.put(TAG_TITLE, title);
                contact.put(TAG_ACRONYM, acronym);
                contact.put(TAG_DESCRIPTION, description);

                // adding contact to contact list
                glossaryList.add(contact);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } else {
        Log.e("JsonParser", "Couldn't get any data from the url");
    }
    return null;
}

【问题讨论】:

  • 粘贴 JSON 示例。

标签: android arrays json parsing url


【解决方案1】:

首先,您的AndroidManifest.xml 中应该有这一行

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

2-nd:您的代码中有拼写错误,例如:test = jsonArr.getJSONObje(str); JsonArray 中没有这样的方法

3-d:你出于某种原因迭代字符串:for (int i = 0; i &lt;= str.length(); i++) {

这里是工作演示代码,您可以根据需要进行升级:

public JSONArray getJSONFromUrl(String url1) {

    String responseBody = "";
    HttpClient client = new DefaultHttpClient();
    HttpGet httpGet = new HttpGet(url1);
    try {
        responseBody = client.execute(httpGet, new BasicResponseHandler());
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        JSONArray jarray = new JSONArray(responseBody);
        return jarray;
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return null;

}

protected Void doInBackground(Void... arg0) {
    JsonParser sh = new JsonParser();
    JSONArray jsonArr = sh.getJSONFromUrl("https://www.descartes.com/rest/glossary-items");

    // looping through All Contacts
    if (jsonArr!= null) {
        for (int i = 0; i < jsonArr.length(); i++) {
            JSONObject contact =  jsonArr.optJSONObject(i);
            if (contact!= null)
                Log.e("jsonArr " + i + ":", contact.toString());
        }
    }
    return null;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-14
    • 1970-01-01
    • 2016-09-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多