* object representation for the key/value pair
* data is separated by a comma
* braces save object
* brackets save array
example:
{" username ":" one hundred and fifty thousand ", "age" : "11", "sex", "male"}
add :
JSON is a lightweight data-interchange format, which is based on a subset of JavaScript.
JSON is JS the string representation of object, it USES the text said a JS object information, nature is a string,
JSON and JS object transfers (here we use the Jackson this without fastjson!)
Var user={
Name: "Sun Weihao,"
Age: 16,
Sex: "male"
};
The console. The log (user);
Js object through. Stringfy (parameters) to get Jason string in the form of var
Var STR=JSON. Stringify (user);
The console. The log (STR);
Use. The parse (parameters) method to convert a json string to js object form
Var obj=JSON. Parse (STR);
The console. The log (obj);
Package com. Example. Fuxi. Controller;
The import com. Example. Fuxi. Pojo. User;
The import com. Fasterxml. Jackson. Core. JsonProcessingException;
The import com. Fasterxml. Jackson. Databind. ObjectMapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@ Controller
Public class UserController {
//produce: the return type of the specified response body and coding -- -- -- -- -- to prevent the code
to return to the page@ RequestMapping (value="https://bbs.csdn.net/json", produces="application/json; Charset=utf-8 ")//String format conversion produces="text/HTML. Charset=utf-8 "
@ ResponseBody
Public String json1 () {
User User=new User (" one hundred and fifty thousand ", "11", "male");
ObjectMapper mapper=new ObjectMapper();//need a Jackson's object mapper, which is a class that can convert the object to string
String userJson=null;
Try {
UserJson=mapper. WriteValueAsString (user);//will be a Java object, converted to string
} the catch (JsonProcessingException e) {
e.printStackTrace();
}
System. The out. Println (userJson);
Return userJson;
}
}
the simplification of the above the following
@ RequestMapping (value="https://bbs.csdn.net/json2", produces="application/json; Charset=utf-8 ")//String format conversion produces="text/HTML. Charset=utf-8 "
@ ResponseBody
Public String json2 () throws JsonProcessingException {
User User=new User (" hyperbole ", "26", "male");
Return new ObjectMapper (.) writeValueAsString (user);
}
the list collection convert json
@ RequestMapping (value="https://bbs.csdn.net/json3", produces="application/json; Charset=utf-8 ")//String format conversion produces="text/HTML. Charset=utf-8 "
@ ResponseBody
Public String json3 () throws JsonProcessingException {
ListList=new ArrayList (a);
Hyperbole list. Add (new User (" 1 ", "26", "male"));
List. The add (new User (" hyperbole 2 ", "25", "male"));
List. The add (new User (" hyperbole 3 ", "24", "male"))
List. The add (new User (" hyperbole 4 ", "23", "male"));
Return new ObjectMapper (.) writeValueAsString (list);
}
through setting the json format configuration file
Text/HTML. Charset=utf-8 & lt;/value> Application/json. Charset=utf-8 & lt;/value>
Text/HTML. Charset=utf-8 & lt;/value> Application/json. Charset=utf-8 & lt;/value>
CodePudding user response:
Please adviseCodePudding user response:
CodePudding user response:
CodePudding user response:
In general use in what placeCodePudding user response:
CodePudding user response:
Honey