没有标题JSONArray的JSONObject

时间:2014-05-05 19:22:56

标签: java android json arrays jsonobject

我在" http://www.somesite.com/json"上有远程JSON代码没有像这样的JSONArray:

 [
    {
        "artist":"Some Artist 1",
        "track":"Some Track 1",
    }

    {
        "artist":"Some Artist 2",
        "track":"Some Track 2",
    }

    {
        "artist":"Some Artist 3",
        "track":"Some Track 3",
    }

    {
        "artist":"Some Artist 4",
        "track":"Some Track 4",
    }

 ]

我无法更改此JSON以添加JSONArray。

我有获取价值的Java代码"跟踪":

SomeActivityCut​​:

try {

  JSONObject jsonarray = JSONfunctions.getJSONfromURL("http://www.somesite.com/json");

                for(int i=0; i < 4; i++){

                    jsonobject = jsonarray.getJSONObject("track");

                    curtrack.setText(jsonobject.getString("track"));
                }

            } catch (JSONException e) {
                Log.e("Error", e.getMessage());
                e.printStackTrace();
            }

这是我获取JSON数据的课程:

JSONfunctions:

public class JSONfunctions {

    public static JSONObject getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONObject jArray = null;

        // Download JSON data from URL
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        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();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONObject(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}

此代码名为FATAL EXEPTION:

  

E / log_tag:解析数据时出错org.json.JSONException:输入结束于   

的字符0

1 个答案:

答案 0 :(得分:3)

首先,这个JSON无效。您的JSON的有效版本应为

[
    {
        "artist": "Some Artist 1",
        "track": "Some Track 1"
    },
    {
        "artist": "Some Artist 2",
        "track": "Some Track 2"
    },
    {
        "artist": "Some Artist 3",
        "track": "Some Track 3"
    },
    {
        "artist": "Some Artist 4",
        "track": "Some Track 4"
    }
]

然后,如果您获得JSONArray,则无法构建JSONObject。我还怀疑你想要一个HttpGet,而不是HttpPost,因为我没有看到你使用body(非强制性)。 所以,这就是我认为你想要你的JSON功能的方式:

public class JSONfunctions {

    public static JSONArray getJSONfromURL(String url) {
        InputStream is = null;
        String result = "";
        JSONArray jArray = null;

        // Download JSON data from URL
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet get = new HttpGet(url);
            HttpResponse response = httpclient.execute(get);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

        } catch (Exception e) {
            Log.e("log_tag", "Error in http connection " + e.toString());
        }

        // Convert response to string
        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();
            result = sb.toString();
        } catch (Exception e) {
            Log.e("log_tag", "Error converting result " + e.toString());
        }

        try {

            jArray = new JSONArray(result);
        } catch (JSONException e) {
            Log.e("log_tag", "Error parsing data " + e.toString());
        }

        return jArray;
    }
}

之后,你只需要迭代你的JSONArray:

JSONArray tracks = JSONfunctions.getJSONfromURL("http://www.somesite.com/json");
if(tracks != null) {

    for(int i = 0; i < tracks.length(); i++) {

        try {

            Log.d("track@" + i, tracks.getJSONObject(i).getString("track"));
        } 
        catch (JSONException e) {

            e.printStackTrace();
        }
    }
}