gpt4 book ai didi

java - 如何在android中通过for循环创建JSON对象并放入JSON数组?

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

我有一个 JSON 对象而不是一个 JSON 数组,我想获取该值并创建新的 JSON 对象并再次放入 JSON 数组中。

这里我的 JSONObject 称为响应

{"data":
[{
"level":"lv1",
"name_indo":"Telinga",
"name_eng":"Ear",
"detail_indo":"Telinga adalah alat indra yang memiliki fungsi untuk mendengar suara yang ada di sekitar kita.\r\n",
"detail_eng":"The ear is the sensory apparatus that has a function to hear the sounds that are around us .\r\n",
"image_url":"\/append_img\/telinga.png”},
{
“level":"lv1",
"name_indo":"Gigi",
"name_eng":"Tooth",
"detail_indo":"Gigi adalah alat yang digunakan untuk mengolah makanan saat kita makan.\r\n",
"detail_eng":"Gigi is a tool used to process food when we eat.\r\n",
"image_url":"\/append_img\/gigi.png"}]
}

这里是我的代码,用于创建一个仅包含 name_eng 值和 image_url 值的新 JSON 数组:

                    try {
JSONArray resultArr = response.getJSONArray("data");

JSONObject filterJson = null;
JSONArray filterArr = null;
for (int i = 0; i < resultArr.length(); i++) {
Log.v(TAG, "run for");
JSONObject finalObj = (JSONObject) resultArr.get(i);
String name_eng = finalObj.getString("name-eng");
filterJson = new JSONObject();
filterJson.put("name_eng", name_eng);

filterArr = new JSONArray();
filterArr.put(filterJson);
}
JSONObject fixedJSON = new JSONObject();
assert filterArr != null;
fixedJSON.put("data", filterArr.toString());
Log.v(TAG, "filterarray : "+fixedJSON);
} catch (JSONException e) {
e.printStackTrace();
}

这是我的 LogCat 错误:

03-22 23:50:42.864 10867-10867/tk.partofbodyapp.partofbodyapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: tk.partofbodyapp.partofbodyapp, PID: 10867
java.lang.NullPointerException: Attempt to invoke virtual method 'org.json.JSONArray org.json.JSONArray.put(java.lang.Object)' on a null object reference
at tk.partofbodyapp.partofbodyapp.startgameActivity$startJson.onPostExecute(startgameActivity.java:191)
at tk.partofbodyapp.partofbodyapp.startgameActivity$startJson.onPostExecute(startgameActivity.java:88)
at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

错误startgameActivity.java:191filterArr.put(filterJson);

我想再次制作一个 JSON:

{"data":
[{
"name_eng":"Ear",
"image_url":"\/append_img\/telinga.png”},
{
"name_eng":"Tooth",
"image_url":"\/append_img\/gigi.png"}]
}

有什么想法吗?

最佳答案

试试这个

try {
JSONArray resultArr = response.getJSONArray("data");
JSONArray filterArr = new JSONArray();
for (int i = 0; i < resultArr.length(); i++) {
Log.v(TAG, "run for");
JSONObject filterJson = new JSONObject();
JSONObject finalObj = (JSONObject) resultArr.get(i);
String name_eng = finalObj.getString("name-eng");
String image_url = finalObj.getString("image_url");
filterJson.put("name_eng", name_eng);
filterJson.put("image_url", image_url);
filterArr.put(filterJson);
}
JSONObject fixedJSON = new JSONObject();
assert filterArr != null;
fixedJSON.put("data", filterArr);
Log.v(TAG, "filterarray : "+fixedJSON);
} catch (JSONException e) {
e.printStackTrace();
}

关于java - 如何在android中通过for循环创建JSON对象并放入JSON数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36158578/

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