通过@RequestBody在Spring MVC中传递多个数据时,JSONObject未得到更新。

huangapple go评论105阅读模式
英文:

Passing multiple data through @Requestbody Spring MVC, JSONobject is not getting updated

问题

在循环中更新 JSON 对象时,我遇到了问题,JSON 对象中的数据被覆盖了。

来自用户界面的 JSON 请求

  1. {
  2. "attribute": [
  3. {
  4. "name": "Program",
  5. "status": "Active"
  6. },
  7. {
  8. "name": "Software",
  9. "status": "Active"
  10. }
  11. ]
  12. }

Java 代码

  1. JSONObject response = new JSONObject();
  2. JSONObject obj = new JSONObject();
  3. JSONArray res = new JSONArray();
  4. int i = 1;
  5. for (AttributeList attr_list : addmodel.getAttribute()) {
  6. response.put("name", attr_list.getAttribute_nm());
  7. response.put("status", attr_list.getCategory());
  8. res.add(response);
  9. System.out.println("inloop--res " + res);
  10. obj.put(i, res); // 问题在这里
  11. System.out.println("inloop--obj " + obj);
  12. i++;
  13. }

输出

  1. {
  2. "1": {
  3. "name": "Software",
  4. "status": "Active"
  5. },
  6. "2": {
  7. "name": "Software",
  8. "status": "Active"
  9. }
  10. }
  11. 数据在两个位置都被覆盖了。

抱歉,我无法放置完整的代码。

英文:

I am facing issue while updating JSON object in loop, I am getting overwritten data in json object.

JSON Request from UI

  1. {
  2. "attribute":[
  3. {
  4. "name":"Program",
  5. "status":"Active"
  6. },
  7. {
  8. "name":"Software",
  9. "status":"Active"
  10. }
  11. ]
  12. }

Java COde

  1. JSONObject response = new JSONObject();
  2. JSONObject obj = new JSONObject();
  3. JSONArray res = new JSONArray();
  4. int i=1;
  5. for (AttributeList attr_list : addmodel.getAttribute()) {
  6. response.put("name", attr_list.getAttribute_nm());
  7. response.put("status", attr_list.getCategory());
  8. res.add(response);
  9. System.out.println("inloop--res "+res);
  10. obj.put(i, res);//issue is here
  11. System.out.println("inloop--obj "+obj);
  12. i++;
  13. }

Output

  1. ["1": {"name":"Software","status":"Active"}, "2":{"name":"Software","status":"Active"}]

Data is getting overwritten in both positions.

Sorry I'm not able to put whole code.

答案1

得分: 0

  1. 在循环中需要再次创建新的 JSONObject response = new JSONObject();用于下一个值
  2. **原因** 由于您没有为列表中的每个值创建新对象先前的引用会被新值替换
  3. JSONObject response = null;
  4. JSONObject obj = new JSONObject();
  5. JSONArray res = new JSONArray();
  6. int i=1;
  7. for (AttributeList attr_list : addmodel.getAttribute()) {
  8. response = new JSONObject();
  9. response.put("name", attr_list.getAttribute_nm());
  10. response.put("status", attr_list.getCategory());
  11. res.add(response);
  12. System.out.println("inloop--res " + res);
  13. obj.put(i, res); // 问题出在这里
  14. System.out.println("inloop--obj " + obj);
  15. i++;
  16. }
英文:

You need to create new JSONObject response = new JSONObject(); again in the loop for next value.
Reason: Since you are not creating a new object for each value in the list, the previous reference is getting replaced by the new value.

  1. JSONObject response = null;
  2. JSONObject obj = new JSONObject();
  3. JSONArray res = new JSONArray();
  4. int i=1;
  5. for (AttributeList attr_list : addmodel.getAttribute()) {
  6. response = new JSONObject();
  7. response.put("name", attr_list.getAttribute_nm());
  8. response.put("status", attr_list.getCategory());
  9. res.add(response);
  10. System.out.println("inloop--res "+res);
  11. obj.put(i, res);//issue is here
  12. System.out.println("inloop--obj "+obj);
  13. i++;
  14. }

huangapple
  • 本文由 发表于 2020年5月5日 03:44:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/61600298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定