java.lang.String无法转换为JSONObject,其中值已转换为JSONObject

时间:2014-08-18 21:23:39

标签: java android json google-play-services google-play-games

这是一个多次被问过的问题,但我没有从他们给出的答案中找到任何解决方案。

基本上我使用GSON为JSON创建类的实例,以便将它们序列化并将其存储到云保存中。

这是代码

Gson gson = new GsonBuilder().disableHtmlEscaping().create();
User user = dbmanager.getUser();
String user_json = gson.toJson(user);
saved_data.put("user", user_json);

dbmanager.getUser();是一个Sql查询,它从android数据库收集用户并将其作为User类的实例返回。

然后我使用load方法加载数据

public void converLoadData(String data) throws ParseException
{
    if (data == null || data.trim().equals("")) return;

    try {
         User user = new User();
         data = data.replace("\\\\", "\\");
         JSONObject obj = new JSONObject(data);
         System.out.println("data: " + obj.toString());
         JSONObject user_object = obj.getJSONObject("user");
         System.out.println("user " + user_object.toString());
        String last_sync = obj.getString("last_sync");
        java.util.Date db_sync = dbmanager.getLastSync(user.getID());
               }
    catch (JSONException ex) {
        ex.printStackTrace();
        Log.e(TAG, "Save data has a syntax error: " + data, ex);
    }
    catch (NumberFormatException ex) {
        ex.printStackTrace();
        throw new RuntimeException("Save data has an invalid number in it: " + data, ex);
    }
}

数据println的返回是这个

data: {"current":"{\"title\":\"Puzzle
2\",\"fnMoves\":[],\"solution_path\":\"puzzles\/2\/CKqbKz5f\/7b1886a261b0400768e75dea91948576.json\",\"puzzlecolors\":[0,0,0],\"puzzle_path\":\"puzzles\/2\/CKqbKz5f\/41a0b30fdfdf6685dd50c6019391cc00.tmx\",\"level_id\":2,\"locked\":false,\"level\":2,\"puzzle_site_id\":2,\"id\":2,\"score\":20,\"fnkeys\":1,\"solved\":false,\"difficulty\":1.0}","solved":"[{\"id\":1,\"puzzle_id\":1,\"puzzle_tries\":10,\"user_id\":1}]","user":"{\"personphoto\":\"https:\/\/lh4.googleusercontent.com\/-5XfDNwK1PwI\/AAAAAAAAAAI\/AAAAAAAAPs8\/C0onA9lyKvY\/photo.jpg?sz=50\",\"google_id\":\"fdgsdfgfgfgsdgsdf\",\"personname\":\"Test
Test\",\"last_sync\":\"2014-08-18
22:12:12\",\"lifes\":3,\"highscore\":13,\"ID\":1}","last_sync":"2014-08-18
22:12:12"}

这是错误。此错误与数据值有关,因为我有反斜杠?我将数据(字符串)传递给JSON对象,然后我试图收集用户元素。使用此JSONObject user_object = obj.getJSONObject(" user");这就是我有错误的地方。有人可以帮我吗?谢谢

org.json.JSONException: Value {"personphoto":"https://lh4.googleusercontent.com/-5XfDNwK1PwI/AAAAAAAAAAI/AAAAAAAAPs8/C0onA9lyKvY/photo.jpg?sz=50","google_id":"104757400111626678244","personname":"George Panayi","last_sync":"2014-08-18 22:12:12","lifes":3,"highscore":13,"ID":1} at user of type java.lang.String cannot be converted to JSONObject

解决方案:我正在使用"删除"删除导致JSON对象格式错误的额外反斜杠和引号的方法

data = data.replace("\\", "");
data = data.replace("\"{", "{");
data = data.replace("}\"", "}");
data = data.replace("\"[", "[");
data = data.replace("]\"", "]");

1 个答案:

答案 0 :(得分:2)

我能够通过稍微清理JSON对象来实现它。我认为这只是一个糟糕的格式错误。

首先,逃脱斜线,它们需要均匀或移除。我删除了那些从双引号中删除的内容,但将它们留在了其他地方。

然后我删除了嵌套对象和数组周围的双引号。 “{...}”到{...}和“[...]”到[...]

似乎是所有必要的。

{"current":{"puzzle_path":"puzzles\/2\/CKqbKz5f\/41a0b30fdfdf6685dd50c6019391cc00.tmx","solved":false,"score":20,"difficulty":1,"level_id":2,"id":2,"fnkeys":1,"title":"Puzzle\n2","level":2,"solution_path":"puzzles\/2\/CKqbKz5f\/7b1886a261b0400768e75dea91948576.json","puzzlecolors":[0,0,0],"puzzle_site_id":2,"locked":false,"fnMoves":[]},"user":{"ID":1,"lifes":3,"google_id":"fdgsdfgfgfgsdgsdf","highscore":13,"last_sync":"2014-08-18\n22:12:12","personname":"Test\nTest","personphoto":"https:\/\/lh4.googleusercontent.com\/-5XfDNwK1PwI\/AAAAAAAAAAI\/AAAAAAAAPs8\/C0onA9lyKvY\/photo.jpg?sz=50"},"solved":[{"puzzle_id":1,"id":1,"user_id":1,"puzzle_tries":10}],"last_sync":"2014-08-18\n22:12:12"}