2015-10-20 149 views
0

这是我的代码:字符串的JSONObject转换返回null

JSONObject arrayobject = new JSONObject(preferences.getString("test", "")); 
String responseobject = arrayobject.getString("array"+index); 
JSONObject object = new JSONObject(responseobject); 

这是ArrayObject的:

{"array0":"{"myarray":[{"innerkey":"innervalue"}],"key":"value"}"} 

这是responseobject(它是一个字符串):

{ 
    "myarray": [ 
    { 
     "innerkey": "innervalue" 
    } 
    ], 
    "key": "value" 
} 

为什么object总是设置为NULL?

请帮我一把!

+1

您确定这是responseobject返回的确切结果吗? – Virthuss

+0

这是'responseobject'的确切结构。.. – mercy

+0

检查首选项是否包含“test”键的值。然后使用key“”array“+ index'检查它是否有值。 – Emil

回答

0

你需要带两个json对象从你的响应中获取json数组和jsonobject。

试试下面的代码

JSONObject jsonObject = new JSONObject(preferences.getString("test", "")); 
JSONObject jsonObject1 = new JSONObject(preferences.getString("test", "")); 
JSONObject jbj; 
JSONArray jsonArray = new JSONArray(jsonObject .getString("myarray")); 

       for (int i = 0; i < jsonArray.length(); i++) 
       { 
        jbj= jsonArray.getJSONObject(i); 
        String innerkey= jbj.getString("innerkey"); 
       } 

String key = jsonObject1 .getString("key"); 

希望它可以帮助你。