current position:Home>Receive the JSON string from the front end, and summarize the methods of parsing and value taking at the back end

Receive the JSON string from the front end, and summarize the methods of parsing and value taking at the back end

2022-04-29 09:06:47Sun Xiao~

Receive from the front end json character string , Summary of back-end analytical value taking methods

We often encounter receiving from the front end in our work json String data in format , Then the back-end is parsed . It happens that today's project is useful to , Here is a summary .

Need to use a jar package , Here is jar Download address of the package

link : Click here to download the required jar package . Extraction code :8tfu

package jsonCh;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class jsonTest {
    
	
	
	
	
	
	public static void main(String[] args) {
    
		
		//1. For example, the reception from the front end is like this json character string , But we can't get it directly name sex phone The corresponding value of , So you have to parse this string ,
		String stu = "{\"name\":\"ss\",\"sex\":\"1\",\"phone\":\"123456789\"}";
		// First convert to JSONObject type 
		JSONObject stuObj = JSON.parseObject(stu);
		// adopt JSONObject Medium getString("key") Method , Get the corresponding value 
		System.out.println("name:"+stuObj.getString("name")+" sex:"+stuObj.getString("sex"));
		

		//2. The string contains an array of , For example, the following string 
		String stuInfo = "{\"classId\":1,\"stuInfo\":[{\"name\":\"aa\",\"sex\":\"1\",\"phone\":\"123456789\"},{\"name\":\"bb\",\"sex\":\"0\",\"phone\":\"987654321\"}]}";
		JSONObject stuInfoObj = JSONObject.parseObject(stuInfo);// First convert to JSONObject type 
		System.out.println(" class id:"+stuInfoObj.getString("classId"));// Get to class id
		String stuInfoStr = stuInfoObj.getString("stuInfo");// Get stuInfo This json character string 
		System.out.println(" Student information collection :"+stuInfoStr);
		JSONArray jsonstuInfo = JSONObject.parseArray(stuInfoStr);// take stuInfo It can be interpreted as json Array 
		for (int i = 0; i < jsonstuInfo.size(); i++) {
    // Traverse student information 
			JSONObject jsonStuInfo = jsonstuInfo.getJSONObject(i);// Take the data according to the subscript , Every data is another JSONObject object , So use JSONObject receive 
			String name = jsonStuInfo.getString("name");
			String sex = jsonStuInfo.getString("sex");
			String phone = jsonStuInfo.getString("phone");
			System.out.println(" The student's name is :"+name+" Gender is :"+sex+" The phone number is :"+phone);
		}
		
		
		
		
		
	}

}


This is the information output from the console :
 This is the information output from the console

So to conclude :
Actually received from the front desk json Format string , It all looks like map, That is, the key value is right , We just need to convert the string to JSONObject, Then get the corresponding value through the key name .
If it's more complicated ,map There's a collection in the , That is to convert the string into JSONObject Of , Then get the string of the collection through the key name , Then turn this string into JSONArray Type of , Then you can traverse the collection

copyright notice
author[Sun Xiao~],Please bring the original link to reprint, thank you.
https://en.qdmana.com/2022/119/202204290627138525.html

Random recommended