gpt4 book ai didi

java - 解析数据 data.JSONException : A JSONObject text must begin with '{' at 1 [character 2 line 1] 时出错

转载 作者:行者123 更新时间:2023-12-01 11:07:01 27 4
gpt4 key购买 nike

我正在使用 jsp 为 REST url 创建 JSON。我在浏览器中得到了很好的输出。我的 JSON 输出是这样的:

{"SUCCESS":1,
"CLIENTS":
[{"COMPANY":"BOOK PALACE, PANBAZAR",
"C_NAME":"",
"ID":"3",
"EMAIL":"pranjalcholadhara662@gmail.com",
"CODE":0}
]
}

当我调用该 url,并尝试解析我的 swing 应用程序中的 url 的输出时,它给了我错误。我使用以下代码来解析 JSON。

List<NameValuePair> params = new ArrayList<>();
params.add(new BasicNameValuePair("EMAIL", mail)) ;
JSONObject json = jParser.makeHttpRequest(YOURUrl, "GET", params);
System.out.println(json.toString());
try {
int success = json.getInt("SUCCESS");

if (success == 1) {
products = json.getJSONArray("CLIENTS");
clients = json.getJSONObject("CLIENTS");
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);

String Company = c.getString("COMPANY");
String Name = c.getString("C_NAME");
String Email = c.getString("EMAIL");
String Code = c.getString("CODE");
String ValidTill = c.getString("VALID_TILL");
String sl = c.getString("ID");
COMPANY = Company;
CNAME = Name;
EMAIL = Email;
CODE = Code;
VALID_TILL = ValidTill;
SL = sl;
}
}
} catch (JSONException e) {
System.err.println(e);
}


public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {

// Making HTTP request
try {
// check for request method
if(method.equals("POST")){
// request method is POST
// defaultHttpClient
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params, "utf-8"));

HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();

}else if(method.equals("GET")){
// request method is GET
HttpClient httpClient = HttpClientBuilder.create().build();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);

HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}

} catch (UnsupportedEncodingException | ClientProtocolException e) {
System.out.println(e);
} catch (IOException e) {

}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
System.out.println("Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
System.out.println("Error parsing data " + e.toString());
}

// return JSON String
return jObj;

}

为什么这会给我带来错误:

Error parsing data data.JSONException: A JSONObject text must begin with '{' at 1 [character 2 line 1]

我的 JSON 可以吗?我在解析时做错了什么吗?我使用 jsp 创建了 json。

最佳答案

响应很可能不包含预期的 JSON。

可能的原因是请求处理期间发生错误,导致服务器返回错误响应,例如错误 HTML 页面。您可以在尝试解析 String json 之前通过检查它的值来检查它。

为了避免这种情况,您应该始终检查响应的 HTTP 状态代码:

 if (httpResponse.getStatusLine().getStatusCode() == 200)
... // extract JSON from response
else
... // error

关于java - 解析数据 data.JSONException : A JSONObject text must begin with '{' at 1 [character 2 line 1] 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32842827/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com