Home > Back-end >  JSONObject and Map to store different values
JSONObject and Map to store different values

Time:05-09

 JSONObject json=new JSONObject (); 
Map The map=new HashMap<> (a);
List List=new ArrayList<> (a);

Json. The put (" list ", a list);
The map. The put (" list ", a list);

List. The add (" a ");
List. The add (" b ");
List. The add (" c ");

System. The out. Println (" map: ");
System. The out. Println (map);

System. The out. Println (" json: ");
System. The out. Println (json);

Output:
The map:
{list=[a, b, c]}
Json:
{" a list ": []}


Why is that?

CodePudding user response:

You point into the HashMap and JSONObject toString () method is know what is the difference between then
Console output or log output, if the object without rewriting the toString () method, the output is similar to the com, xiaoxiao. Test. Model. Tom @ 2 e5d6d97, object comspec @ hashCode, hashCode for memory address; If objects override the toString () method, which is in accordance with the toString () method of the output,

CodePudding user response:

reference 1/f, one born in response:
you point into the HashMap and JSONObject toString () method to know what is the difference then
Console output or log output, if the object without rewriting the toString () method, the output is similar to the com, xiaoxiao. Test. Model. Tom @ 2 e5d6d97, object comspec @ hashCode, hashCode for memory address; If objects override the toString () method, which is in accordance with the toString () method of the output,

I mean why json is not stored in value

CodePudding user response:

I copy your code, the output of the deposit, I is
The map:
{list=[a, b, c]}
Json:
{" a list ": [" a", "b", "c"]}

CodePudding user response:

reference IAM_YXQ reply: 3/f
Quote: reference 1/f, get a response in raw:
you point into the HashMap and JSONObject toString () method to know what's the difference between then
Console output or log output, if the object without rewriting the toString () method, the output is similar to the com, xiaoxiao. Test. Model. Tom @ 2 e5d6d97, object comspec @ hashCode, hashCode for memory address; If objects override the toString () method, which is in accordance with the toString () method of the output,

I mean why json is not stored in value

When you when the output is the two lines
System. The out. Println (" json: ");
System. The out. Println (json);
Before these three lines:
List. The add (" a ");
List. The add (" b ");
List. The add (" c ");
Order cannot reverse, otherwise the output will be []

CodePudding user response:

used one to five floor response:
Quote: refer to the third floor IAM_YXQ response:
Quote: reference 1/f, get a response in raw:
you point into the HashMap and JSONObject toString () method to know what is the difference then
Console output or log output, if the object without rewriting the toString () method, the output is similar to the com, xiaoxiao. Test. Model. Tom @ 2 e5d6d97, object comspec @ hashCode, hashCode for memory address; If objects override the toString () method, which is in accordance with the toString () method of the output,

I mean why json is not stored in value

When you when the output is the two lines
System. The out. Println (" json: ");
System. The out. Println (json);
Before these three lines:
List. The add (" a ");
List. The add (" b ");
List. The add (" c ");
Order cannot reverse, otherwise the output will be []


Order not reverse, the output is empty, I am using jdk1.8

CodePudding user response:

Served, when you put the list into json, the list is empty, add data to the list, behind the json: of course not.

CodePudding user response:

reference 7 floor of seven of the world, I'll take ninety-nine response:
took, you take your list into json, the list is empty, add data to the list, behind the json: of course not.

Inside the map is a reference object, object changed, content will change, but not as json.

CodePudding user response:

refer to 6th floor IAM_YXQ response:
Quote: refer to the 5 floor one born to reply:
Quote: refer to the third floor IAM_YXQ response:
Quote: reference 1/f, one born in response:
you point into the HashMap and JSONObject toString () method to know what is the difference then
Console output or log output, if the object without rewriting the toString () method, the output is similar to the com, xiaoxiao. Test. Model. Tom @ 2 e5d6d97, object comspec @ hashCode, hashCode for memory address; If objects override the toString () method, which is in accordance with the toString () method of the output,

I mean why json is not stored in value

When you when the output is the two lines
System. The out. Println (" json: ");
System. The out. Println (json);
Before these three lines:
List. The add (" a ");
List. The add (" b ");
List. The add (" c ");
Order cannot reverse, otherwise the output will be []


Order did not reverse, the output is empty, I am using jdk1.8

Can all the content in the hair under your code, including the import guide packages, as well as fastjson version

CodePudding user response:

Which version of the json package?
Fastjson 1.2.47 jdk1.8 measurement are,
Feel you run the code and your post code is different,

CodePudding user response:

JSONObject json=new JSONObject ();
Map The map=new HashMap<> (a);
List List=new ArrayList<> (a);

Json. The put (" list ", a list);
The map. The put (" list ", a list);

List. The add (" a ");
List. The add (" b ");
List. The add (" c ");

System. The out. Println (" map: ");
System. The out. Println (map);

System. The out. Println (" json: ");
System. The out. Println (json);
-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
If you are sure this code, arguably should be can output the
Because you are a collection of objects add elements in the map and json toString (), you can try the original poster

CodePudding user response:

For List< is saved; String> List=new ArrayList<> (a); A reference to the
  • Related