Js reads Json, JSON and string conversion, and Java reads JSON

Source: Internet
Author: User
1. When JS reads JSON and writes AJAX, it is often necessary to parse a string returned from the server. Here we will briefly introduce the two formats of the characters returned by the server and the parsing methods of JS for them. JSON is the js object tag (JavaScriptObjectNotation). It is a method for declaring objects in JS... SyntaxHighlighter.

1. JS read JSON

When writing AJAX, it is often necessary to parse a string returned from the server. Here we will briefly introduce two formats of characters returned by the server and the parsing methods of JS for them.

JSON is the JavaScript Object Tag (JavaScript Object Notation). It is a string that is combined by declaring objects in JS.
JS can define objects as follows:
Var obj =

{
Id: 2,
Name: 'n'
};

Alert (obj. name );

In this way, the object obj is defined. It has two common attributes: id and name. You can directly access its attribute values using obj. id.

When obtaining data from the server, there is usually more than one object, which requires an array of objects. The array of objects in JS can be defined using [], as shown below:


Var objs = [{id: 1, name: 'n' _ 1'}, {id: 2, name: 'n' _ 2'}];
Alert (objs [0]. id );
In this way, the object array objs is defined. It contains two objects and can be accessed using indexes. For example, objs [0] Will reference the first object. Here you may have thought about the format of the string returned by the server, but the string is a string after all, and we need to convert it to a variable that can be operated by JS. This uses the eval function. See the following example:

Var objs = eval ("[{id: 1, name: 'n' _ 1'}, {id: 2, name: 'n' _ 2'}]");
Alert (objs [0]. id); // return 1
All right, on the server side, you only need to use the format [{id: 1, name: 'n' _ 1'}, {id: 2, name: 'n' _ 2'}] returns a string. on the client side, you can use eval () to execute the returned string to obtain an array of objects. The following is a simple example using AJAX. The code for retrieving json with js is as follows:

Var objs = eval (request. responseText );
Alert (objs. length); // 2
Alert (objs [0]. id); // 1
Alert (objs [1]. name); // 'n' _ 2'

Add a test button to view the effect:

 

However, sometimes the Json data is spliced by itself, and is directly read using js, and it is found that the read data is undefined... that is to say, like

Var s = "{name1: 'name1', name2: 'name2'}"; js treats it as a string rather than JSON data. Therefore, we need to convert it into JSON data.

Note: If you write tmp = eval (tmp) at this time, the error "missing;" will be reported. It should be: tmp = eval ("(" + tmp + ")");
Then use document. getElementById ("txt1"). value = tmp. name1.


Another method for converting strings to JSON: // json. js is required.

Var obj = strJSON. parseJSON ();
Var obj = JSON. parse (strJSON );

Convert JSON to a string:

Var str = obj. toJSONString ();
Var str = JSON. stringify (obj)

2. Java reads JSON data:

Import java. io .*;
Import org. json .*;
Public class Demo {
Public static void main (String [] args) throws Exception {
String str = "{\" a \ ": \" B \ ", \" c \ ": \" d \"}";
JSONObject a = new JSONObject (str );
System. out. println (a); // {"c": "d", "a": "B "}
System. out. println (a. get ("c"); // d
}
}

Just give the string as a parameter to JSONObject ......


Excerpted from the column of wandering hacker
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.