在 Java 中的 JSON 对象中使用 JSON 数组

Use JSON Array inside JSON object in Java

我有一个包含 2 到 6 整数值的数组列表,我测试每个数字是奇数还是偶数,如下所示:

JSONObject outerObject = new JSONObject();
JSONArray outerArray = new JSONArray();
JSONObject [] innerObject = new JSONObject[list1.size()];
        for ( i = 0; i < list1.size(); i++)// change to list1.size()
            {
                innerObject[i]=new JSONObject();
                innerObject[i].put("Item" , list1.get(i));
                if(list1.get(i)%2==0)
                {
                    innerObject[i].put("Odd or Even", "number "+list1.get(i)+" is even");
                }
                else
                    innerObject[i].put("Odd or Even", "number "+list1.get(i)+" is odd");                  
                outerArray.put(innerObject[i]);
            }
        outerObject.put("rows", outerArray);
        return outerObject.toString();

然后我有以下 JSON 文件结果

{"rows":[
{"Item":2,"Odd or Even":"number 2 is even"},
{"Item":3,"Odd or Even":"number 3 is odd"},
{"Item":4,"Odd or Even":"number 4 is even"},
{"Item":5,"Odd or Even":"number 5 is odd"},
{"Item":6,"Odd or Even":"number 6 is even"}
]}

现在我想找到数组列表项之间的乘法 喜欢:

{"rows":[
    {"Item":2,"Odd or Even":"number 2 is even", "Multiplication:[4,6]"},
    {"Item":3,"Odd or Even":"number 3 is odd","Multiplication:[6]"},
    {"Item":4,"Odd or Even":"number 4 is even"},
    {"Item":5,"Odd or Even":"number 5 is odd"},
    {"Item":6,"Odd or Even":"number 6 is even"}
    ]}

我知道我需要在第一个循环中使用另一个 for 循环,像这样

for (j=i; j < list1.size(); j++)
            {
                inerArray[j]=new JSONArray();
                if (list1.get(j)%list.get(i)==0)
                    inerArray[j].put(list1.get(j));

            }
innerObject[i].put("Multiplication" , inerArray);

但是没用

我正在使用 Java 和 Restful 网络服务

我解决了你的问题。你可以试试这个。

public static void main(String[] args) throws JSONException {
    List<Integer> list1 = new ArrayList<>();
    list1.add(2);
    list1.add(3);
    list1.add(4);
    list1.add(5);
    list1.add(6);
    JSONObject outerObject = new JSONObject();
    JSONArray outerArray = new JSONArray();
    JSONObject[] innerObject = new JSONObject[list1.size()];
    for (int i = 0; i < list1.size(); i++)// change to list1.size()
        {
        innerObject[i] = new JSONObject();
        innerObject[i].put("Item", list1.get(i));
        if (list1.get(i) % 2 == 0) {
            innerObject[i].put("Odd or Even", "number " + list1.get(i) + " is even");
        } else{
            innerObject[i].put("Odd or Even", "number " + list1.get(i) + " is odd");
        }
        //Finding Multiplication
        int currentNumber = list1.get(i);
        String mulitplcationNumbers = "[";
        for(Integer numberFromList : list1){
            if(numberFromList !=currentNumber && numberFromList % currentNumber==0){
                mulitplcationNumbers+=numberFromList + ",";
            }
        }
        if(!mulitplcationNumbers.equals("[")){
            mulitplcationNumbers = mulitplcationNumbers.substring(0, mulitplcationNumbers.length()-1);
            innerObject[i].put("Multiplication", mulitplcationNumbers + "]");
        }
        outerArray.put(innerObject[i]);
    }
    outerObject.put("rows", outerArray);
    System.out.println(outerObject.toString());
}

如果对你有用,你可以试试这个:-

public static void main(String args[]){
    List<Integer> list1 = new ArrayList<Integer>();
    list1.add(2);
    list1.add(3);
    list1.add(4);
    list1.add(5);
    list1.add(6);
    JSONObject outerObject = new JSONObject();
    JSONArray outerArray = new JSONArray();
    List<Integer> duplicateValue=new ArrayList<Integer>();
    JSONObject [] innerObject = new JSONObject[list1.size()];
            for (int  i = 0; i < list1.size(); i++)// change to list1.size()
                {
                    JSONArray inerArray = new JSONArray();
                    innerObject[i]=new JSONObject();
                    innerObject[i].put("Item" , list1.get(i));
                    if(list1.get(i)%2==0)
                    {
                        innerObject[i].put("Odd or Even", "number "+list1.get(i)+" is even");
                    }
                    else{
                        innerObject[i].put("Odd or Even", "number "+list1.get(i)+" is odd"); 
                    }
                    for (int j=i; j < list1.size(); j++)
                    {
                        if(list1.get(j)!=list1.get(i)){
                            if (list1.get(j)%list1.get(i)==0){
                                    if(!duplicateValue.contains(list1.get(j))){
                                    inerArray.add(list1.get(j));
                                    duplicateValue.add(list1.get(j));
                                }
                            }
                        }
                    }
                    if(inerArray.size()!=0){
                        innerObject[i].put("Multiplication" , inerArray);
                    }
                    outerArray.add(innerObject[i]);
                }

            outerObject.put("rows", outerArray);
            System.out.println(outerObject.toString());
}

OutPut :- {"rows":[{"Item":2,"Odd or Even":"number 2 is even","Multiplication":[4,6]},{"Item":3,"Odd or Even":"number 3 is odd","Multiplication":[6]},{"Item":4,"Odd or Even":"number 4 is even"},{"Item":5,"Odd or Even":"number 5 is odd"},{"Item":6,"Odd or Even":"number 6 is even"}]}