Want to know javascript convert json string to object? we have a huge selection of javascript convert json string to object information on alibabacloud.com
JSON string:var str1 = ' {' name ': ' cxh ', ' sex ': ' Man '} ';JSON object:var str2 = {"Name": "Cxh", "Sex": "Man"};To use the above str1, you must first convert to a JSON object using the following method:Convert from JSON
Suppose we have obtained a Java object on the server side, and now we are considering how to convert this Java object into a string that complies with the JSON syntax. Of course, we can write a set of algorithms to implement this conversion, but it is troublesome, especially
advanced browsers such as FIREFOX,CHROME,OPERA,SAFARI,IE9,IE8 can directly use the stringify () and Parse () methods of JSON objects.Json.stringify (obj) converts the JSON to a string. Json.parse (String) to convert the string to
Assuming that the server has returned a JSON-formatted string to the browser, how can we convert this special string into a JS object?
We can use one of the prototype LibrariesFunction evaljson ()To convert a
catch blockIf (LOG. isInfoEnabled ())LOG.info ("the date format may be invalid and the current date of the system will be used ");E. printStackTrace ();}Return new java. SQL. Timestamp (System. currentTimeMillis (); // return the default date}@ OverridePublic Class morphsTo (){// TODO Auto-generated method stubReturn Timestamp. class;}}
Final Version:[Java]Package com. defshare. foundation. global;Import java. SQL. Timestamp;Import java. text. ParseException;Import java. text. SimpleDateFormat;
Method 1: Use the JS extension method if (typeof s = = 'objectnullreturnreturn /^ (string|number) $/. Test (typeof s)? "'" + S + "' for in O" Arr.push ("'" + i + "':return Method 2: Add a reference to the Json2.js var jsondetail = { Content: " I love Beijing Tian ' an gate", Type: "1" };If we want to convert the JSON
Convert a JSON string to a JSON object.
Applicable hotel O2O menu
Var stri = [// member center {"menucode": 1, "menuname": "member center" ,}, {"menucode": 1, "name ": "Member query", "perm_id": 1, SubsMenu: [{"perm_id": 1, "menueurl": "Customer/Index", "menuename ": "Member
such as {"tag": "Sendcode", "data": {"phone": "18880488738"}}--> First observe the key value pairs, the above a total of three key value pairs, the bottom is "phone": "18880488738", the above layer is "data": {"Phone": "18880488738"} and " Tag": "Sendcode",1, you should first define a JSON object to install the underlying "phone": "18880488738"-->jsonobject js1=new jsonobject ();Js1.put ("Phone", "188
transitional//en">Basehref="">'personinfo.jsp'Starting Page"pragma"Content="No-cache">"Cache-control"Content="No-cache">"Expires"Content="0">"keywords"Content="Keyword1,keyword2,keyword3">"Description"Content="This is my page">"stylesheet"Type="Text/css"href="Styles.css">-->dynamically get the directory for the current project's ContextPath: ${pagecontext.request.contextpath}"javabeantojosnstring"> Send request to servlet Json_ 0 Base _005_ to convert
C # convert json to a dynamic object, and then dynamically obtain the value in the specified string list in the dynamic object,
Using Newtonsoft. Json;Using System;Using System. Collections. Generic;Using System. Linq;Using System
/** * Divide the object into JSON string (JSON + recursive) * @param the obj parameter should be {@link Java.util.Map} or {@link java.util.List} * @return */@SuppressWarnings ("unchecked") Public Staticobject Jsonenclose (Object obj) {Try { if(obj instanceof M
When I called the PHP function that returned the array value in JS a few days ago, I did not know how to read the returned PHP array data in JS. So I thought of converting the PHP array into a JSON string first, then, get the returned value in JS and convert it to an object using the eval function of Js. to read data.
Instance one,
In JS, the JSON-formatted string is converted to a JSON object, and the key code
The code is as follows
Copy Code
JSON = eval (' (' +str+ ') ');
The method is as follows:
The code is as follows
Copy Code
JSON is a lightweight data interchange format that is easy to use. Easy for people to read and write. It is also easy to machine parse and generate. For details, please see: http://www.json.org/.
Many times we need to assemble a string as a JSON object, first combining the string
What is JSONJSON (JavaScript Object Notation) is a graceful way to create JavaScript objects. JSON is also a lightweight data interchange format. JSON is very easy for people to read and write, while facilitating machine parsing and generation.
})
};
Var str = '';
For (var key in result ){
If (typeof result [key] === 'string '){
Str + = key + ": '" + result [key] + "' \ n ";
} Else {
Str + = key + ":" + result [key] + "\ n ";
}
}
Console. log (str );
}
Test_toStringify ();
The output result is as follows:Copy codeThe Code is as follows: JSON. stringify (undefined): undefinedJSON. stringify (null): 'null'JSON. stringify (123): '20140901'
toString (){ returnname+ ":" +Age}}Entities are simple, two fields, and of course the fields in an entity can be list or set type.Gson Gson= NewGson ();List Person>Persons= NewArrayList Person>(); for (intI= 0; I Ten; I++) {Person P= NewPerson ();P.setname ("name" +i);P.setage (i* 5);Persons.add (P);}String Str=Gson.tojson (persons);The above code focuses on the Gson object, which provides the Tojas
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.