0%

解析JSON内容异常(JSONException)

解析JSON内容异常(JSONException)

error : {} net.sf.json.JSONException: JSONObject[“aliUserId”] not found.


​ 原因是请求参数没有这个元素

1
2
RestResponse data = thirdPartyMemberService.queryMemberCode(requestObject.getInt("brandId"), requestObject.getString("phoneNo"), requestObject.getString("aliUserId"), requestObject.getString("openId"));

​ JSON中没有这个key,更没有这个key所对应的值,因此需要加入为空判断,如果没有这个字段对应的key添加key和对应值。

1
2
3
4
5
6
7
8
9
10
11
if (!requestObject.has("phoneNo")){
requestObject.put("phoneNo","");
}
if (!requestObject.has("aliUserId")){
requestObject.put("aliUserId","");
}
if (!requestObject.has("openId")){
requestObject.put("openId","");
}
RestResponse data = thirdPartyMemberService.queryMemberCode(requestObject.getInt("brandId"), requestObject.getString("phoneNo"), requestObject.getString("aliUserId"), requestObject.getString("openId"));

​ 这样请求参数中的非必填项可只填任意项


全部都填写:

1
2
3
4
5
6
7
8
9
{
"access_token": "",
"ciphertext": {
"phoneNo": "",
"openId": "",
"aliUserId": "",
"brandId":
}
}

只填某一项:

1
2
3
4
5
6
7
{
"access_token": "150581bb5d0f4f25986267e52a3e263f",
"ciphertext": {
"phoneNo": "",
"brandId":
}
}