2012-05-20 213 views
1

我可以从json_encode从PHP的JSON响应,我能够证明它在我的Eclipse的logcat什么反应是:Android:字符串不能转换为JSONArray?

[{"idusers":"1","full_name":"Test Subject","get_email":"[email protected]"}, 
{"idusers":"2","full_name":"Test Subject_2","get_email":"[email protected]"}] 

现在,我想

//parse json data 
    try { 
     JSONArray jArray = new JSONArray(result); 
     for(int i =0; i<jArray.length(); i++){ 
      JSONObject json_data = jArray.getJSONObject(i); 
      Log.i("log_tag","id: "+json_data.getInt("idusers")+ 
        ", Full Name: "+json_data.getString("full_name")+ 
        ", Email: "+json_data.getString("get_email") 
        ); 
     } 
    }catch(JSONException e){ 
     Log.e("log_tag","Error parsin data "+e.toString()); 
    } 

然而,我发现了一个错误,说

Error parsin data org.json.JSONException: Value testing of type java.lang.String cannot be converted to JSONArray

是有办法解决的JSONArray问题?

在此先感谢!

+1

有一个“你的字符串结束时失踪。这是你的问题的原因,还是只是一个错字? –

+0

http://stackoverflow.com/questions/9441932/android-json-error-expected-begin-object-but-was-begin-array-at-line-1-column-2 –

+0

@RajeshJAdvani a typo – hellomello

回答

1

找到发生了什么事,我有PHP文件中的字符串,是不是被称为,我有单词“测试”,这是错误的单词。一旦这个“测试”被删除,它的工作。谢谢

0

尝试将转换添加到由JSON返回的PHP变量。另外,你是否使用json_encode?

$user_id = (int) $id; 
$email = (string) $email; 
+0

我是($ get = $ SQL-> fetch(PDO :: FETCH_ASSOC)){ \t \t $ output [] = $ get;}使用json_encode在我的php – hellomello

+0

这是我通过我的PHP文件' \t} \t \t print(json_encode($ output));' – hellomello

+0

所以,我在技术上并没有真正设置任何变量,它只是直接朝向数组。 mysql中的数据类型是varchar,是否有区别?和id是int。 – hellomello

1

您的JSON无效。引号(“)中缺少‘中的第2个数组元素get_email’值

应该是:。

[ 
    { 
     "idusers": "1", 
     "full_name": "Test Subject", 
     "get_email": "[email protected]" 
    }, 
    { 
     "idusers": "2", 
     "full_name": "Test Subject_2", 
     "get_email": "[email protected]"}] 
+0

这绝对只是一个错字。因为我从缓冲读取器得到响应 – hellomello