When the app interacts with H5, there is a need to adjust the original sharing. There is no problem in the interaction, because sharing requires multiple values, so json is used for transmission. When the app receives and parses, it encounters this parsing exception Value of type java.lang .String cannot be converted to JSONObject`
This may be a process of continuous accumulation, and we look forward to making progress together~
JSONException: Value of type java.lang.String cannot be converted to JSONObject (String cannot be converted to JSONObject)
这个异常其实很常见,但是导致这种异常的场景可能各有不同 ~
Scenarios encountered by everyone on the Internet (solution)
Due to the problem caused by the headerutf-8
of , the header needs to be removed (someone also tried to process the string and remove the header)=bom
bom
json
bom
无效,无兴趣可跳过!!!
json
There is no problem with the string format, so 可以肯定的是json字符内部有无法识别或者未知的字符
someone did the following processing ( 根据json数据不同,过滤条件也不同
)
String jsonStr = httpTools.doGet("URL接口地址",paramsBaseList);
String json = jsonStr.substring(jsonStr.indexOf("{"), jsonStr.lastIndexOf("}") + 1);
or as follows
public String JSONTokener(String in) {
// consume an optional byte order mark (BOM) if it exists
if (in != null && in.startsWith("\ufeff")) {
in = in.substring(1);
}
return in;
}
JSONObject obj = new JSONObject(JSONTokener(jsonStr));
Personal conclusion: First of all, I haven't figured out what this bom
head is... Secondly, there is one thing that is useful →json字符内部有无法识别或者未知的字符
最终 如下的处理方式帮到了我!!! 不过要注意是否处理下过期方法...
- Kotlin
val parser = JsonParser()
val retVal: String = parser.parse(it).asString
- Java
JsonParser parser = new JsonParser();
String retVal = parser.parse(param).getAsString();
Look Here :然后再使用JSONObject解析 retVal 即可
Scenario where I encountered the problem (solved)
First explain the pseudo code I share
- The method provided by the app to the front end
/**
* 分享
*/
val shareInfo = MutableLiveData<String>()
@Keep
@JavascriptInterface
fun appShare(param: String?, callbackId: String?) {
Timber.e("appShare---> callbackId -->${
callbackId} --param-->${
param}")
if (param.isNullOrEmpty()) return
shareInfo.postValue(param.string())
}
- The logic after js calls the app, mainly see
jsonObject
the parsing part
javascriptMethod.shareInfo.observe(viewLifecycleOwner) {
params ->
val jsonObject = JSONObject(params)
val url: String = jsonObject.optString("url", "")
val title: String = jsonObject.optString("title", "")
val desc: String = jsonObject.optString("desc", "")
val icon: String = jsonObject.optString("icon", "")
activity?.let {
ShareService.service?.share(it, url, title, desc, icon) }
}
- The json string obtained by the app
这样的json串和我们平时接口返回的json串有所不同,但它也是正常的json串,所以需要处理内部的特殊字符,如 \ 、"" 等
"{\"url\":\"https://baidu.com\",\"title\":\"测试JS方法\",\"desc\":\"测试H5调用APP分享页功能~~~\",\"icon\":\"https://mobile.baidu.com.cn/actapp/files/upload/images/32c72f230622.png\"}"
icon
- Handle special json strings and solve problems
/**
* 分享
*/
val shareInfo = MutableLiveData<Map<String, String?>>()
@Keep
@JavascriptInterface
fun appShare(param: String?, callbackId: String?) {
Timber.e("appShare---> callbackId -->${
callbackId} --param-->${
param}")
if (param.isNullOrEmpty()) return
try {
val startIndex = if (param.startsWith("\"")) 1 else 0
val endIndex = if (param.endsWith("\"")) param.length - 1 else param.length
val result = param.substring(startIndex, endIndex).replace("\\", "")
val jsonObject = JSONObject(result)
val url = jsonObject.optString("url")
val title = jsonObject.optString("title")
val desc = jsonObject.optString("desc")
val icon = jsonObject.optString("icon")
if (url.isNullOrEmpty()) return
shareInfo.postValue(mapOf("url" to url, "title" to title, "desc" to desc, "icon" to icon))
} catch (throwable: Throwable) {
Timber.e(throwable)
}
}