-1

我可以连接到服务器并获取 json 数据。它是这种格式:

    {"clazz":"ManiaContestantList","contestants":[

    {"clazz":"ManiaContestant","contestantId":"1","contestantName":"Adira","photoUrl":"/fileFeed.action?service=astroManiaService&action=viewFile&type=JPG&path=1/1_CONTESTANT_PHOTO.png"},
    {"clazz":"ManiaContestant","contestantId":"2","contestantName":"Akim","photoUrl":"/fileFeed.action?service=astroManiaService&action=viewFile&type=JPG&path=2/2_CONTESTANT_PHOTO.png"},
.
.
.
    ]}

我的代码是这样的:

@Override
    protected void onStart() {
        super.onStart();
        Log.i("NewsList", "inside onStart();");

        try {
            URL url = new URL("/MY URL/");
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(Manager.ConnTimeout);
            conn.setReadTimeout(Manager.ReadTimeout);

            int responseCode = conn.getResponseCode();
            Log.i("Connection oppened", "Response code is:" + responseCode);
            if (responseCode == HttpURLConnection.HTTP_OK) {
                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                if (in != null) {
                    StringBuilder strBuilder = new StringBuilder();
                    // Read character by character              
                    int ch = 0;
                    while ((ch = in.read()) != -1)
                        strBuilder.append((char) ch);

                    // get returned message and show it
                    strServerResponseMsg = strBuilder.toString();
                    Log.i("Data returned by server:", strServerResponseMsg);



                    JSONArray jObjects = new JSONArray(strServerResponseMsg);
                    for(int i=0; i<jObjects.length(); i++){
                        System.out.println(jObjects.getJSONObject(i).getString("contestantId").toString());
                        System.out.println(jObjects.getJSONObject(i).getString("contestantName").toString());
                        System.out.println(jObjects.getJSONObject(i).getString("photoUrl").toString());
                }
                in.close();
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }       


    }

我在“strServerResponseMsg”变量中有我的响应 Json。但在“ JSONArray jObjects = new JSONArray(strServerResponseMsg);”行中,Logcat 显示typemismatch错误。

请告诉我我的问题是什么?

4

4 回答 4

4

{暗示它是一个对象。[暗示它是一个数组。由于您的字符串以它开头,{所以它是一个 JSONObject 而不是 JSONArray。

JSONArray jObjects = new JSONArray(strServerResponseMsg);

一定是这样的

JSONObject jObjects = new JSONObject(strServerResponseMsg);
于 2012-04-05T05:05:45.143 回答
1

给出的 json 示例不是 jsonarray 它是 jsonobject 所以你应该像下面这样

JSONObject jObj = new JSONObject(strServerResponseMsg);
JSONArray jArray = new JSONArray(jObj.getJSONArray("contestants"));

然后遍历 jArray

于 2012-04-05T05:00:56.660 回答
0

这是你得到的 JSONObject,'{' 很明显。

  JSONObject j=null;

    try{
 j=new JSONObject(strServerResponseMsg);
}
catch(JSONException r)
{
    //Handle it.
    r.printStackTrace();


 }
 Log.w("json",j.toString());
于 2012-04-05T05:15:11.487 回答
0

JSON是回溯的,如果你使用json你必须非常小心处理。因此,使用谷歌开发的 GSON api。它是在 json api 的帮助下制作的。请访问此链接 - 单击此处

于 2012-04-05T06:20:10.920 回答