Home > Back-end >  Help, how to create an empty json object, then the circulation data in json (every three data into a
Help, how to create an empty json object, then the circulation data in json (every three data into a

Time:10-26

I want to create a json object in front, there are multiple data {id: num: price}, for example, how to put more of these data in the same json objects and how to create the json object is?

Now I will only in the two data below, now see a lot of information on the net, json is a little bit confused,

CodePudding user response:

You can start to build an object or a Map to your id, num, price as the key attribute of an object or a Map, and then the object with library and Map into a json format string output response

CodePudding user response:

 
/* *
* generate json string objects
*
* @ param & lt; T>
* @ param t
* @ return
*/
Public static & lt; T> String toJson (T T) {
Objects. RequireNonNull (t);
Return new Gson (). ToJson (t);
}
/* *
* generate json string Map
*
* @ param map
* @ return
*/
Public static String toJson (Map Map) {
If (null==map | | map. The isEmpty ()) {
Return "{}";
}
Type gsonType=new TypeToken () {}. GetType ();
Return new Gson (). ToJson (map, gsonType);
}
Public static String toStatsJson (Map Map) {
If (null==map | | map. The isEmpty ()) {
Return "{}";
}
Type gsonType=new TypeToken () {}. GetType ();
Return new Gson (). ToJson (map, gsonType);
}