与JSON问题传输Android数据



这是我使用PHP创建的JSON数组。我想知道Bellow JSON数组具有正确的语法。如何使用Android获取值,因为我的示例代码会收到一些错误。

这是JSON数组genarating

的PHP脚本
public static function getCategory($_lgtime) {
        $con = JsonDataManip::connect();
        $stmt = $con->prepare("select * from " . _TABLE_CATEGORY . " where lgtime > ?");
        $stmt->execute(array($_lgtime));
        while ($row = $stmt->fetch()) {
            $jsonArray['key'] = $row['key'];
            $jsonArray['name'] = $row['name'];
            $jsonArray['lgtime'] = $row['lgtime'];
            $json[] = $jsonArray;
        }
        return $json;
    }

用法php:

echo json_encode(JsonDataManip::getCategory('20140129184514895'));

genarated json数组

[{
    "key":"1",
    "name":"Category 10",
    "lgtime":"20140129184514896"
    },
    {
    "key":"2",
    "name":"Category 9",
    "lgtime":"20140129184514896"
    },
    {
    "key":"3",
    "name":"Category 8",
    "lgtime":"20140129184514896"
    }]

Android JSON PARSER函数

protected Void doInBackground(Void... arg0) {
            ServiceHandler sh = new ServiceHandler();
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
            Log.d("Response: ", "> " + jsonStr);
            if (jsonStr != null) {
                try {
                    JSONObject  jsnArry = new JSONObject(jsonStr);
                JSONArray jsonArray = jsnArry.getJSONArray("");
                for(int i=0;i<jsnArry.length();i++){
                    Log.d("JSON PARSE",jsonArray.getJSONObject(i).getString("name"));
                    //contactList[i] =          
                }
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            return null;
        }

错误

01-30 08:45:53.527: W/System.err(1507): org.json.JSONException: Value {"lgtime":"20140129184514896","key":"1","name":"Category 10"} at 0 of type org.json.JSONObject cannot be converted to JSONArray

您的PHP脚本返回JSON数组,而不是JSON对象。您应该这样更新您的Android代码:

if (jsonStr != null) {
    try {
        JSONArray json = new JSONArray(jsonStr);
        for (int i=0; i < json.length(); i++) {
            Log.d("JSON PARSE", json.getJSONObject(i).getString("name"));
        }
    } catch (JSONException e) {
        Log.w("JSON PARSE", "Failed to parse JSON!", e);
    }
}

检查您的JSON字符串是否有效,请使用此链接JSONLINT。就您的代码而言,您的错误显然指出

type org.json.JSONObject cannot be converted to JSONArray

可能的解决方案(如果您使用的是GSON)

您对ServiceHandler的响应将是字符串。因此,将其转换为JSONObject,然后将其通过到JSONArray

示例

JSONObject jsonObject = new JSONObject(<responseString>);
JSONArray jsonArray = jsonObject.getJSONArray();

其他

JSONObject jsonObject = new JSONObject(<responseString>);
String key = jsonObject.getJSONObject("key");
String name = jsonObject.getJSONObject("name");
String lgtime = jsonObject.getJSONObject("lgtime");

然后您可以在程序中继续进行逻辑。

{    "employees":[
    {
    "firstName":"John",
    "lastName":"Doe"
    },
    {
    "firstName":"Anna",
    "lastName":"Smith"
    },
    {
    "firstName":"Peter",
    "lastName":"Jones"
    }
    ]
}

缺少您的开口和闭合括号。这不是有效的JSON数组,以上是示例数组格式

最新更新