gpt4 book ai didi

java - 在 Java 上获取 XML 或 Json 的代码

转载 作者:行者123 更新时间:2023-12-01 09:01:57 25 4
gpt4 key购买 nike

我正在尝试获取来自这些*链接*的数据
http://21.26.54.26/swapi/api/data/json
http://21.26.54.26/swapi/api/data/xml

我尝试了很多方法,但最终当它经过这一行时总是给我错误 JSONObject jsonObj = new JSONObject(json);

 OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url("http://********/swapi/api/data/xml")
.build();


Response responses = null;

try {
responses = client.newCall(request).execute();

json = responses.body().string();

System.out.println("JSON: " + json);

try {


JSONObject jsonObj = new JSONObject(json);


// JSONObject obj = new JSONObject(jsonObj);

// Getting JSON Array node
JSONArray contacts = jsonObj.getJSONArray("Carro");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString("idintelisis");
System.out.println("TESTEEEEEEEEEEEEEEEEEEEEEEE: " + id);


}
} catch (JSONException ex) {

System.out.println("Errrrrrrrrrrrrrrrrrrrrrrrrorrrrrrrrrrrrrrrrrrr: " + ex);
}


} catch (IOException e) {
e.printStackTrace();
}

这是当该行中断时抛出的错误 ** long **

01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: org.json.JSONException: Value [{"idintelisis":101,"observaciones":"Nitin","fecha":null,"carro":null,"cargapkt":"sd5"},{"idintelisis":101,"observaciones":"Nitin","fecha":null,"carro":null,"cargapkt":"sd5"}] of type java.lang.String cannot be converted to JSONObject 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at org.json.JSON.typeMismatch(JSON.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at org.json.JSONObject.(JSONObject.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at org.json.JSONObject.(JSONObject.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at pa.com.tropigas.pedidostf.CarrosActivity$POST.doInBackground(CarrosActivity.java:114) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at pa.com.tropigas.pedidostf.CarrosActivity$POST.doInBackground(CarrosActivity.java:65) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf W/System.err: at java.lang.Thread.run(Thread.java) 01-11 10:04:47.487 6541-7024/pa.com.tropigas.pedidostf I/System.out: Error: org.json.JSONException: Value [{"idintelisis":101,"observaciones":"Nitin","fecha":null,"carro":null,"cargapkt":"sd5"},{"idintelisis":101,"observaciones":"Nitin","fecha":null,"carro":null,"cargapkt":"sd5"}] of type java.lang.String cannot be converted to JSONObject

我使用** Postman **检查** Json **的结果以及**日志**的** Json **,一切都表明** Json **在术语上是正确的我不明白尝试将其转换为 * Json * 对象时会发生什么问题 *

最佳答案

因为你得到的是 JsonArray 而不是 JsonObject

试试这个

JsonArray arr = new JsonArray(obtained_result);

然后尝试使用 getJSONObject(index) 方法从 arr 获取 JsonObject

示例:

JsonArray arr = new JsonArray(obtained_result);
for(int i = 0; i< arr.length(); i++){
JsonObject ob = arr.getJSONObject(i);
}

关于java - 在 Java 上获取 XML 或 Json 的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41597982/

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