简体   繁体   English

Android数据传输与Json问题

[英]Android data transferring with Json issue

This is my json array creating with php. 这是我用php创建的json数组。 I want to know bellow json array have correct syntax. 我想知道下面的json数组具有正确的语法。 How to get the values withing android,because my sample code get some errors. 如何通过android获取值,因为我的示例代码出现了一些错误。

THIS IS PHP SCRIPT FOR JSON ARRAY GENARATING 这是JSON数组生成的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;
    }

usage php : 用法php:

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

GENARATED JSON ARRAY 生成的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 FUNCTION ANDROID JSON解析器功能

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;
        }

ERROR 错误

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

To check if your JSON string is valid, please use this link JSONLint . 要检查您的JSON字符串是否有效,请使用此链接JSONLint As far your error is concerned for your code, it clearly states that 至于您的错误与您的代码有关,它明确指出:

type org.json.JSONObject cannot be converted to JSONArray

Possible Solution (if you are using Gson) 可能的解决方案(如果您使用的是Gson)

Your response from ServiceHandler would be a string. 您从ServiceHandler得到的响应将是一个字符串。 So convert that into a JSONObject and the pass that to JSONArray . 因此,将其转换为JSONObject并将其传递给JSONArray

Example

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

Else 其他

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

Then you can continue with your logic in the program. 然后,您可以继续执行程序中的逻辑。

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

Your opening and closing braces are missing. 您的左括号和右括号都丢失了。 that is not a valid json array, The above is a sample array format 这不是有效的json数组,上面是示例数组格式

Your PHP script returns a JSON array, not a JSON object. 您的PHP脚本返回JSON数组,而不是JSON对象。 You should update your Android code like so: 您应该这样更新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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM