This article mainly introduces why eval in JS needs to enclose JSON data. For more information, see the rise of Ajax, the lightweight JSON data format gradually becomes popular as the transmission format between the client and the server. the problem that arises is how to convert the JSON data built on the server to av
JSON = JavaScript Object notationWhat is JSONThe text format of the data interchange, simply stated, is that each JSON object is a value, either a value of a simple type or a value of a composite type, but only one value, not two or more values, which means that each JSON document can contain only one value.The advanta
Previously just know the format of JSON, also done is to get the data from the database and then into the JSON format and then assign to the Highcharts generation curve, first the database data using the array () function to convert the arrays, and then use Json_encode () The function converts the array format to JSON
This article mainly introduces 2 methods of String (string) to JSON in JavaScript, two methods are using the JS function eval () and, using the Jquery.parsejson () method, the need for friends can refer to the
The first way:
Use the JS function eval ();
Testjson=eval (Testjson); Is the wrong way to convert.
The correct conversion method needs to be added ():
The example in this article describes the way JavaScript parses JSON. Share to everyone for your reference. The specific methods are as follows:
Download the JSON libraryHttp://www.json.org/json-zh.html himself for JavaScript.Or just go down there.Https://github.com/douglascrockford/
If you are currently using Restful APIs and you need to construct a json string response using a web project, this article will help you create json strings using javascript. This is very useful. We will use the jQuery plug-in $. toJSON to convert the data object to the json
JavaScript-implemented query JSON instance code:Below is a snippet of code strength for a better JavaScript query JSON on a network, hopefully helping you with your needs.The code example is as follows:/** Define template functions*/ varTemplate =function(Queryarr) {varCount = 0; for(vari = 0; i ) {
Today, I read an article about converting the XML format to the JSON format by David Walsh. It feels good and I simply reprinted it.
The following is the magic javascript code for converting XML to JSON:// Changes XML to JSONFunction xmlToJson (xml ){// Create the return objectVar obj = {};If (xml. nodeType = 1) {// element// Do attributesIf (xml. attributes. len
)? "" + S + "'": S;
}
for (var i in O)
arr.push ("' + i +" ': "+ FMT (O[i]));
Return ' {' + arr.join (', ') + '} ';
I used it anyway, haha.
Also: If you do not want the numbers in JSON to be serialized. Can be modified: return/^ (String|number) $/.test (typeof s)? ' "' + S + '" ': s; is: Return/^ (String) $/.test (typeof s)? ' "' + S + '" ': s; (It's just ignoring the number type.)
function o2string (O) {
//return jso
": "Yanqing district" },{ "name": "Huairou district" },{ "name ": "Pinggu district" },{ "name": "Miyun district" },{ "name": "Yizhuang Development Zone"}]
Reply content:
After php retrieves data from the database for processing $ this-> ajaxReturn ($ data );The following data is obtained, but the js end requires the name and sub to remove the quotation marks, {name: "Beijing "}In js, the typeof type is object.Convenient processing in Php or convenient processing in js,How are they handled.
[{"N
To combine data into a JSON-formatted stringvar text = ' {' sites ': [' + ' {' name ': ' Runoob ', ' url ': ' www.runoob.com '}, ' + ' {' name ': ' Google ', ' url ': ' Www.google.c Om "}, ' + ' {" name ":" Taobao "," url ":" Www.taobao.com "}]} ';Converts a string into a JavaScript object using the JavaScript built-in function json.parse () in a
The first way:
Use the JS function eval ();
Testjson=eval (Testjson); Is the wrong way to convert.
The correct conversion method needs to be added (): Testjson = eval ("+ Testjson +"));
Eval () is very fast, but he can compile and execute any JavaScript program, so there are security issues. The eval () is used. The source must be trustworthy. You need to use a more secure
{Code ...} {code ...} {code ...} {code ...} the code above refers to the hexadecimal code obtained through PHP conversion. However, when JavaScript gets the code, it has become a string and you want to convert cmd to the hexadecimal Blue part like cmd1 for the desired effect. red indicates the data content.
var cmd = ["0x02,0x03,0xB4,0xB4,0x00,0x06","0x00,0x02,0x6E,0x00,0x00,0x05","0x01,0x01,0x00,0x78,0x00
This article focuses on the jsurl parameter value. js obtains the parameter value in the url based on the key value and converts the url parameter to json. if you are interested, let's take a look, this article is not a good place to write. I hope to point out a lot of comments and suggestions, and paste the code directly, example 1 shows how JS obtains the parameter values in the URL based on the key value and converts the URL parameters to
This article focuses on the jsurl parameter value. js obtains the parameter value in the url based on the key value and converts the url parameter to json. If you are interested, let's take a look, this article is not a good place to write. I hope to point out a lot of comments and suggestions, and paste the Code directly, example 1 shows how JS obtains the parameter values in the URL based on the key value and converts the URL parameters to
Method 1: splice JSON format in front-end JavaScript, array format (or set) [{},{}], object format {"":"","": ""} {Name :{"":""}}
Use List
Listwebsituser is a generic set of recommedwebsiteuser. In this way, the JSON string spliced at the front end is converted to a list of collections.
[Serializable]
Class recommedwebsiteuser{Private string userid;
Pu
Detailed explanation of json parsing examples using javascript
Json format generated by php
Usage page
The Code is as follows:
Alert (data. toJSONString ());
If false is returned, no data is returned.Js Code:
The Code is as follows:FunctionshowJSON (){
Varuser =
{
"Username": "tom ",
"Age": 20,
"Info": {"tel": "123456", "cellphone": "98765 "},
"Address ":
[
{"C
/** * Get data items for table tables.*/functionGettabledata (tableId) {vardata = "{";//Defining Data Variables$ ("#" + tableId). Find ("input, select"). each (function(){//iterate through the tabs of input, select, etc. in a table if($( This). attr ("id")) {//If this tag is set with an ID, the data is taken outData + = "\" "+ $ ( This). attr ("id") + "\": \ "" "+ $ ( This). Val () + "\", ";//stitching ID and data } }); if(Data.length! = 1) {//If you remove the data, delete the
{"Name": "Monkey", "Age": "a", "Height": 164.0}
If you want to make the above JSON string more readable on the page, it becomes the following style:
{
"name": "Monkey",
"age": "A",
"height": 164.0cm
}
The method described in this article is based on JavaScript, and the code is as follows:
Where Window.error_id_msgs is the JSON object to
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.