1. Use @requestBody String json , mark applicationCoent='json'
2. Need to import
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.1</version>
</dependency>
New file *.txt
{
"table1":"t_userA",
"status":"a",
"content":[
{"id":1,"ide":"eclipse","name":Java},
{"id":2,"ide":"XCode","name":"Swift"},
{"id":3,"ide":"Visual Stdio","name":"C#"}
],
"table2":"t_userB",
"status":"d",
"content":[
{"id":1,"ide":"eclipse","name":Java},
{"id":2,"ide":"XCode","name":"Swift"},
{"id":3,"ide":"Visual Stdio","name":"C#"}
],
"table3":"t_userC",
"status":"u",
"content":[
{"id":1,"ide":"eclipse","name":Java},
{"id":2,"ide":"XCode","name":"Swift"},XCode","name":"Swift"},XCode","name":"Swift"},
{"id":3,"ide":"Visual Stdio","name":"C#"}
]
}
test test class
import java.io.FileNotFoundException;
import java.io.FileReader;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import com.google.gson.JsonArray;
import com.google. gson.JsonIOException;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
note that null pointers need to be judged
public class actimqTest {
public static void main(String[] args ) {
//Convert test.json data into JSON object
//Need to create a parser, which can be used to parse string or input stream
String jsonString ="{\"table\":\"t_user\",\" stauts\":\"a\",\"context\":\"[email protected]\"}";
String str= "{\"table\":\"t_user\",\"stauts\":\"a\",\"context\":"
+ "[{\"table\":25,\"gender\":\"female\",\"grades\":\"Three shifts\",\"name\":\"Lucy\"} ],\"flag\":\"true\"}"; try { JsonParser parser=new JsonParser(); //Create a JSON object and receive the parsed return value of the parser JsonObject object=(JsonObject) parser.parse( new FileReader("D:\\test.txt")); //Create JsonObject object System.out.println("table="+object.get("table1").getAsString()); System.out.println ("stauts="+object.get("status").getAsString()); //Create a JsonArray JsonArray array=object.get("content").getAsJsonArray(); /* if(array.size() !=0&&array!=null){ }*/ //Judge JSON,Whether the length of the list collection is greater than 0 proves that there is data if(CollectionUtils.sizeIsEmpty(array)) { for (int i = 0; i < array.size(); i++) {
System.out.println("-----------------"+array.get(i).toString());
//Create a JsonObject and get it from the subscript of the array ,get ( ) returns JsonElement
type "id").getAsString()); System.out.println("ide="+subObject.get("ide").getAsString()); System.out.println("name="+subObject.get( "name").getAsString()); } } } catch (JsonIOException e1) { e1.printStackTrace(); } catch (JsonSyntaxException e2) { e2.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto- generated catch block e.printStackTrace();}}}