我有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:191
为filterArr.put(filterJson);
我想再次制作一个JSON:
{"data":
[{
"name_eng":"Ear",
"image_url":"\/append_img\/telinga.png”},
{
"name_eng":"Tooth",
"image_url":"\/append_img\/gigi.png"}]
}
有什么想法吗?
答案 0 :(得分:3)
试试这个
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();
}
答案 1 :(得分:1)
嗯,你不用担心,这是一个简单的方法,只需5个简单的步骤即可完成。
1:创建模型POJO - 仅包含输出json所需的字段。
public class Model2 {
String name_eng;
String image_url;
//Create Getters and Setters
}
2:从json创建Json对象 - 输入是你的json字符串
JSONObject jsonObject = new JSONObject(json);
3:获取JSONArray并将其转换为对象列表
Type listType = new TypeToken<ArrayList<Model2>>() { }.getType();
List<Model2> outputList = new Gson().fromJson(jsonObject.getJSONArray("data").toString(), listType);
(使用Gson将json转换为Object。也必须是一些使用JSON库的好方法)
4:创建新的JSONObject - 发送响应
JSONObject newJsonObject = new JSONObject();
5:将outputList添加到newJsonObject
newJsonObject.put("data", outputList);
PS:你的json是不可序列化的,直到你做了更改,我在评论中对你说。
答案 2 :(得分:0)
问题是你每次通过循环都会创建一个新的JSONArray。还要确保您获得了要过滤每个循环迭代的所有信息。
try {
JSONArray resultArr = response.getJSONArray("data");
JSONObject filterJson = null;
JSONArray filterArr = new JSONArray();
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");
String image_url = finalObj.getString("image_url");
filterJson = new JSONObject();
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.toString());
Log.v(TAG, "filterarray : "+fixedJSON);
} catch (JSONException e) {
e.printStackTrace();
}