JSON取值过程中迭代次数频繁重复的问题咨询
Hey there, let's break down why you're seeing repeated iterations when generating JSON from your QuestionaryVo list. Here are the most likely culprits and how to fix them:
1. Duplicate Entries in Your Source List
First, let's rule out the simplest cause: is the list returned by questionaryService.getQuesTemp(questempId) already containing duplicate QuestionaryVo objects?
Add a quick check before your loop to verify:
List<QuestionaryVo> questionaryList = questionaryService.getQuesTemp(questempId); System.out.println("Total items fetched: " + questionaryList.size()); // Print each entry to spot duplicates for (QuestionaryVo vo : questionaryList) { System.out.println("Question EN: " + vo.getQuestDesc_en()); }
If you see repeated values here, the problem lies in your service layer's query logic—maybe your SQL query isn't using DISTINCT, or a join is creating duplicate rows.
2. Accidental Repeated Additions to JSON Arrays
Looking at your code snippet, you've initialized jsonArray and jsonArray1 but haven't shown where you're adding the data object to these arrays. If you're calling jsonArray.put(data) multiple times inside the loop (e.g., copying the line by mistake, or adding to both arrays), you'll end up with duplicate entries in your final JSON.
Fix this by ensuring you only add each data object once to your target array:
// Inside your loop JSONObject data = new JSONObject(); data.put("questDesc_en", quesTemp.getQuestDesc_en()); // Add other fields as needed... jsonArray.put(data); // Only one call to put() per iteration
3. Confusing Variable Naming (Shadowing Risk)
You named your list variable getQuesTemp—the exact same name as your service method. While Java allows this, it's easy to accidentally reassign or reference the wrong thing later in the code. Rename the variable to something clear like questionaryList to avoid this confusion entirely.
4. Iterator Misuse (Less Likely, But Worth Checking)
While your iterator usage looks correct so far, double-check that you're not calling itr.next() more than once per loop iteration. Multiple calls to next() would skip entries or cause unexpected repetition if you're reusing the same object reference incorrectly.
Cleaned-Up Code Example
Here's a streamlined version of your code to avoid these pitfalls:
String getQuesTemp(@PathVariable Integer questempId) throws JSONException{ JSONObject jsonObject = new JSONObject(); JSONArray jsonArray = new JSONArray(); // Rename variable for clarity List<QuestionaryVo> questionaryList = questionaryService.getQuesTemp(questempId); // Use enhanced for-loop for simpler, safer iteration for(QuestionaryVo quesTemp : questionaryList) { JSONObject data = new JSONObject(); data.put("questDesc_en", quesTemp.getQuestDesc_en()); // Add other fields here... jsonArray.put(data); } jsonObject.put("questions", jsonArray); return jsonObject.toString(); }
Start with verifying the source list for duplicates—that's the most common root cause here. If the list is clean, move on to checking your JSON array insertion logic.
内容的提问来源于stack exchange,提问作者Ragul N

