如何通过从java中的String数组或ArrayList获取值来创建所需的JSON结构?

时间:2018-05-30 04:59:23

标签: java json arraylist dialogflow

我尝试在java中创建一个所需的json结构但是我在json结构中只获得了最后一个值,因为我在for循环中创建了json。如果我在嵌套for循环中创建它,那么它不会给我想要的json结构。

我的代码在这里: -

  public static void main(String[] args) {
        JSONObject TP1 = new JSONObject();
        String[] alias = {"topping","cake"};
        String[] entityType = {"Topping","cake"};
        String[] textString = {"pizza","pancake"};
        String[] usersays_text = {"I want ","I want "};

        for(String usy:usersays_text)  
        {
             TP1.put("text",usy.toString());
           }

           JSONObject TP2 = new JSONObject();
           for(String tS:textString)   
           {
               TP2.put("text",tS.toString());
           }
           for(String eT:entityType)   
           {
               TP2.put("entityType",eT.toString());
           }
           for(String al:alias)    
           {
               TP2.put("alias",al.toString());
           }
           JSONArray JSA=new JSONArray();
           JSA.put(TP1);
           JSA.put(TP2);

           JSONObject root1= new JSONObject();
             root1.put("parts", JSA);

             JSONArray JSA4=new JSONArray();
             JSA4.put(root1);
             JSONObject root3= new JSONObject();
             root3.put("TP", JSA4);
             //To print
             JSONObject json = new JSONObject(root3.toString()); // Convert text to object
             System.out.println(json.toString(4)); 
      }

导致我跟随 json结构: -

      {
    "TP": [{
        "parts": [{
                "text": "I want "
            },
            {
                "entityType": "cake",
                "alias": "cake",
                "text": "pancake"
            }
        ]
    }]
  }

字符串数组的每个值的所需结构 - 例如:

{
 "TP": [
    {
      "parts": [
        {
          "text": "I want "
        },
        {
          "alias": "topping",
          "text": "pizza",
          "entityType": "Topping"
        }
      ]
    },
    {
      "parts": [
        {
          "text": "I want "
        },
        {
          "alias": "cake",
          "entityType": "cake",
          "text": "pancake"
        }
      ]
    }
  ]
}

2 个答案:

答案 0 :(得分:1)

你必须使用单循环,假设每个数组具有相同的长度,以下代码可以帮助你

public static void main(String[] args) throws Exception {

        String[] alias = {"topping", "cake"};
        String[] entityType = {"Topping", "cake"};
        String[] textString = {"pizza", "pancake"};
        String[] usersays_text = {"I want ", "I want "};

        JSONArray parts = new JSONArray();

        for (int i = 0; i < usersays_text.length; i++) {
            JSONArray JSA = new JSONArray();
            JSONObject TP1 = new JSONObject();
            TP1.put("text", usersays_text[i]);
            JSONObject TP2 = new JSONObject();
            TP2.put("text", textString[i]);
            TP2.put("entityType", entityType[i]);
            TP2.put("alias", alias[i]);
            JSA.put(TP1);
            JSA.put(TP2);
            parts.put( JSA);
        }



        JSONObject partsObject = new JSONObject();
        partsObject.put("parts",parts);
        JSONObject root= new JSONObject();
        root.put("trainingPhrases", partsObject);
        //To print
        JSONObject json = new JSONObject(root.toString()); // Convert text to object
        System.out.println(json.toString(4));
    }

答案 1 :(得分:1)

您的jsonobject构造错误。在&#39; jsonobject&#39;密钥是唯一的

当你这样尝试时

for(String usy:usersays_text)  
{
   TP1.put("text",usy.toString());
}

相同的密钥出现在json对象中,值被替换
请尝试下面的代码,它按预期构造json对象。

public static void main(String args[]) {
JSONObject TP1 = new JSONObject();
String[] alias = {"topping","cake"};
String[] entityType = {"Topping","cake"};
String[] textString = {"pizza","pancake"};
String[] usersays_text = {"I want ","I want "};

JSONObject jobj = new JSONObject();
JSONArray jarr = new JSONArray();


for(int index = 0; index < usersays_text.length; index++)   
{
    JSONObject parts = new JSONObject();
    JSONArray partsArr = new JSONArray();
    JSONObject partsObj = new JSONObject();
    partsObj.put("text", usersays_text[index].toString());
    JSONObject cont = new JSONObject();
    cont.put("alias", alias[index].toString());
    cont.put("text", textString[index].toString());
    cont.put("entityType", entityType[index].toString());
    partsArr.put(partsObj);
    partsArr.put(cont);
    parts.put("parts", partsArr);
    jarr.put(parts);
}

jobj.put("trainingPhrases", jarr);
System.out.println(jobj.toString(4)); 
}