Java JsonObject array value to key

前端 未结 3 1501
有刺的猬
有刺的猬 2021-01-03 09:53

I\'m new to java so this is a bit confusing

I want to get json formatted string

The result I want is

{ \"user\": [ \"name\", \"lamis\" ] }
         


        
3条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-03 10:50

    Another way of doing it is to use a JSONArray for presenting a list

       JSONArray arr = new JSONArray();
       arr.put("name");
       arr.put("lamis");
    
       JSONObject json = new JSONObject();
       json.put("user", arr);
    
       System.out.println(json);   //{ "user": [ "name", "lamis" ] }
    

提交回复
热议问题