The value of Java jsonobject array is key

I'm new to Java, so it's a little confusing

I want to get a string in JSON format

What I want is

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

What I am doing now is:

JSONObject json = new JSONObject();         
json.put("name","Lamis");
System.out.println(json.toString());

And I got the result

{"name":"Lamis"}

I tried this, but it didn't work put(“user”,json.put(“name”,“Lamis”));

Solution

Try this:

JSONObject json = new JSONObject();         
json.put("user",new JSONArray(new Object[] { "name","Lamis"} ));
System.out.println(json.toString());

However, the "wrong" result you show will be "there is a more natural mapping of users named" LAMIS "than the" correct "result

Why do you think the "right" result is better?

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>