我正在向服务器发送 POST 请求。服务器使用以下 (JSON) 进行响应:
{"data":[
{
"password":"1234578566",
"status":"processing"
}
],
"status":200
}
这是我的 POST 方法:
public static void sendPostRequest(String conversion) throws IOException, AuthenticationException {
CloseableHttpClient client = HttpClients.createDefault();
HttpPost httpPost = new HttpPost("https://url.com");
httpPost.setEntity(new StringEntity(conversion));
UsernamePasswordCredentials credentials =
new UsernamePasswordCredentials("username", "password");
httpPost.addHeader(new BasicScheme().authenticate(credentials, httpPost, null));
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(httpPost);
String data = EntityUtils.toString(response.getEntity());
client.close();
// System.out.println(data);
}
请注意,字符串“data”是响应上述 JSON 数据的服务器。现在,我试图从数据中获取密码属性。
public static void getValue(String data) throws ParseException {
JSONObject object = (JSONObject) new JSONParser().parse(data);
JSONArray array = (JSONArray) object.get("data");
JSONObject attribute = (JSONObject) array.get(0);
JSONObject userData = (JSONObject) attribute.get("password");
String result = userData.toString();
System.out.println(result);
}
Exception in thread "main" Unexpected character (T) at position 0.
at org.json.simple.parser.Yylex.yylex(Unknown Source)
at org.json.simple.parser.JSONParser.nextToken(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
at org.json.simple.parser.JSONParser.parse(Unknown Source)
我收到此异常,但我想知道为什么?曾试图在这里和那里改变,但没有成功。
这些是我的进口:
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;
import org.json.simple.parser.ParseException;
import org.json.simple.parser.JSONParser;
谢谢你。
森栏
相关分类