Js and jquery parses json and array formats in detail

  • 2020-03-30 01:17:41
  • OfStack

Before parsing, we must figure out a few concepts: what are the differences and contact points between arrays, associative arrays, and json?

I. concept introduction
An array of 1.

ECMAScript v3 specifies the syntax for array direct quantities, and JavaScript 1.2 and JScript 3.0 implement it. You can create and initialize an array by placing a comma-separated list of expressions in square brackets. The values of these expressions become array elements. Such as:

Var a = [1, true, 'ABC '];

See the API for specific actions.

Ps: must be separated by square brackets.

2. Associative arrays

1. Grammar:
Var myhash= {" key1 ":" val1 ", "key2" : "val2"}; / / obj

2. The var
Myhash = {key1: "val1", key2: "val2"}; / / obj - can also

Ps: it's almost the same as json, but the json format is much more rigorous (the key-value pairs inside must use double quotes), but json can only be used as a format standard and must be converted into associative array objects (obj) if you want to manipulate it.

2. Easy operation
1. Add keys to the Hash associative array

// add a newkey, newkey, and the key value is newval

Myhash newkey "] [" = "newval";

2. Delete the existing key values of the Hash associative array

// deletes a key, newkey, and the corresponding newval disappears
The delete myhash newkey "] [";

3. Traverse the Hash associative array

// traverses the entire hash array
For (key in myhash) {
Val = myhash [key];

4. Get the value

Methods 1. Myhash. Key1
Way 2. Myhash. Key2

3. The json
Format requirements:

{" key1 ", "val1", "key2" : "val2"}; // strictly in this format, the operations can follow the operations of the associative array

Two. A few key points in front and background interaction
1. When the data sent by the server is not a single json, but multiple jjson, the array and associative array should be used to assemble the string
For example: var objs = [{id: 1, name: "n_1"}, {id: 2, name: 'n_2}].

2. The data from the server to the client is just a string from start to finish, so it can be converted into a js executable object with eval() in order for it to perform the necessary operations in js.
Therefore, the $.parsejson () provided in jQuey is limited. In the case mentioned in 1 above, you must use eval () to convert it, and then pass $.each (objs, function (I, o) {... }) to operate

Three. Specific example code
Page code:

 <input type="button" value="send ajax json" onclick="sendAjaxByjson();"/>
 <input type="button" value="send ajax array" onclick="sendAjaxByarray();"/>
 <script type="text/javascript">
  function sendAjaxByjson(){
    var obj=data;
    alert(typeof obj);//string
    //Var a = eval (obj); Don't understand, do not comment will report an error..
    var strToobj=$.parseJSON(obj);
    alert(typeof strToobj)//obj
    var obja={'name':'techbirds','age':'23','sex':'male'};
    alert(typeof obja);//obj
    delete obja['name'];
  function sendAjaxByarray(){
    var str=data;
    alert(typeof str);//string
    alert(typeof eval(str));//object
    var obja=[1,2,3,4,5];
    alert(typeof obja);//object


Background code:

 protected void service(HttpServletRequest req, HttpServletResponse reps)
   throws ServletException, IOException {
  Map<String, Object> jsonMap=new HashMap<String, Object>();
  jsonMap.put("name", "techbirds");
  jsonMap.put("age", 23);
  jsonMap.put("sex", "male");


 protected void service(HttpServletRequest req, HttpServletResponse reps)
   throws ServletException, IOException {
  String array="[1,2,3,4,5,6]";


Related articles: