类型java.lang.String的值无法转换为JSONObject-Android

时间:2018-09-18 17:29:57

标签: android json

见过this thread可能的解决方案,但它击败了我。
我将客户数据对象作为JSON字符串存储在共享首选项(Android)中,并尝试检索转换回客户对象的字符串并用于验证。从对象到JSON字符串的转换非常好,并且存储在首选项中(作为键值(字符串)对)。
当我尝试使用SharedPreferences.getString(key,"")创建JSONObject或数组(不是数组,但还是尝试过)时,总是收到相同的错误“ java.lang.String类型的 Value ...转换为JSONObject ”。
我希望另一双眼睛能捕捉到我无法捕捉到的东西。 带有数据的错误消息(已屏蔽):

{"address":{"city":"city","country":"country","customer_address_id":0,"customer_id":0,"house_number":"#123, Lane 1, Street 1","latitude":0.0,"longitude":0.0,"postcode":"12001","street":"Lane 2, Street 2"},"ageGroup":"25-45","dateOfBirth":"1537308474000","email":"abc@abc.com","firstName":"abcdefg","gender":"","id":"108","lastName":"xyz","locale":"en_us","middleName":"none","phone":"1234567890","uuid":"8c3ce2c5-600f-3c4e-bc07-727d61fae7ff"}
类型为java.lang.String的

无法转换为JSONObject。
我想要做的只是(代码要点):
使用以下方法保存到共享首选项:

jsonAdapter = moshi.adapter(RegisterUser.class);
mRegisterUser = (RegisterUser) jsonAdapter.fromJson(regisCustUser.toString());
prefs = UserPrefs.getUserPrefs(getApplicationContext());
prefs.setPrefsItem(UserPrefs.getregUserKey(), jsonAdapter.toJson(mRegisterUser));`  

其中setPrefsItem执行以下操作:

JsonAdapter jsonAdapter = moshi.adapter(javaObject.getClass());
String cartJson = jsonAdapter.toJson(javaObject);
// Log.e("CartJsonreflection", cartJson);
editor.putString(prefKey, cartJson);
editor.commit();

在这里进行检索:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

...
private static SharedPreferences settings; 

...
public static Object someMethod(String prefKey, Object javaObject) {
    String prefJson = settings.getString(prefKey, "");
    JSONObject regisCustomer = new JSONObject(prefJson);
    javaObject = (someObject) jsonAdapter.fromJson(regisCustomer.toString()); 
    // casting is redundant but added for clarity
    return javaObject;
}

其中prefJson具有上述字符串。 我试图在两个网站上验证JSON字符串-https://jsoneditoronline.org/https://jsonlint.com/ 我什至没有在记事本++中粘贴任何特殊字符的字符串,但没有找到。

3 个答案:

答案 0 :(得分:0)

尝试以下

Gson gson = new GsonBuilder().create();
public static <T> T someMethod(String prefKey, Class<T> type) {
   String prefJson = settings.getString(prefKey, "");
   return gson.fromJson(prefJson , type);
}

答案 1 :(得分:0)

可能是您尝试将默认string的空字符串转换为JSONObject的情况。 确保您通过{获得了prefJson中的值,并确保已使用正确的密钥初始化了SharedPreferences,并且getString()的密钥也正确。

    var s ="{\"address\":{\"city\":\"city\",\"country\":\"country\",\"customer_address_id\":0,\"customer_id\":0,\"house_number\":\"#123, Lane 1, Street 1\",\"latitude\":0.0,\"longitude\":0.0,\"postcode\":\"12001\",\"street\":\"Lane 2, Street 2\"},\"ageGroup\":\"25-45\",\"dateOfBirth\":\"1537308474000\",\"email\":\"abc@abc.com\",\"firstName\":\"abcdefg\",\"gender\":\"\",\"id\":\"108\",\"lastName\":\"xyz\",\"locale\":\"en_us\",\"middleName\":\"none\",\"phone\":\"1234567890\",\"uuid\":\"8c3ce2c5-600f-3c4e-bc07-727d61fae7ff\"}";
    val regisCustomer = JSONObject(s)
    println(regisCustomer.toString())

因为使用您的值对字符串进行硬编码就可以了。

答案 2 :(得分:0)

解决方案包括来自其他post的答案。 我必须做:

   prefJson= prefJson.replace("\\\"", "\"");
   prefJson = prefJson.substring(prefJson.indexOf("{"), prefJson.lastIndexOf("}") + 1);

古怪的是,sharedpreferences的确切字符串如下所示(包括值中的开始和结束引号('“')。不确定为什么这是一个问题,仅当作为参数传递时)。引号和转义硬编码到字符串变量中时,字符会被忽略:

"{\"address\":{\"city\":\"City\",\"country\":\"Country\",\"customer_address_id\":0,\"customer_id\":0,\"house_number\":\"#123, Lane 1, Street 1\",\"latitude\":0.0,\"longitude\":0.0,\"postcode\":\"PA 12345\",\"street\":\"Lane 2, Street 2\"},\"ageGroup\":\"25-45\",\"dateOfBirth\":\"1537368909000\",\"email\":\"abc@abc.com\",\"firstName\":\"First Name\",\"gender\":\"\",\"id\":\"119\",\"lastName\":\"Last Name\",\"locale\":\"en_us\",\"middleName\":\"none\",\"phone\":\"0987654321\",\"uuid\":\"8c3ce2c5-600f-3c4e-bc07-727d61fae7ff\"}"  

必须转换为:

{"address":{"city":"City","country":"Country","customer_address_id":0,"customer_id":0,"house_number":"#123, Lane 1, Street 1","latitude":0.0,"longitude":0.0,"postcode":"PA 12345","street":"Lane 2, Street 2"},"ageGroup":"25-45","dateOfBirth":"1537368909000","email":"abc@abc.com","firstName":"First Name","gender":"","id":"119","lastName":"Last Name","locale":"en_us","middleName":"none","phone":"0987654321","uuid":"8c3ce2c5-600f-3c4e-bc07-727d61fae7ff"}  

注意:主持人,如果听起来很重复,请随时删除或合并此问题。谢谢大家。