the white busy record and share the problem. recently like to encapsulate the return value of the interface into a JSON string;The advantages are obvious, especially convenient to adjust and modify the return value of the interface.
let's start with the question:
after the Ajax request succeeds, a JSON string is returned as follows:
{xdata:["经济发展","名生保障","生态环保","平安建设","脱贫攻坚","其他"],ydata:[24,15,15,5,0,0],zdata:[0,2,0,0,1,0]}
It was not a standard JSON string, so I used the json.stringify () to go down,originally thought to have been a standard JSON string; then start calling JSON. Parse intends to convert to a JSON object. The discovery did not succeed, or a string (not)but I call eval ("(" + Data + ")"); It was a simple turn to success.
Reason:
key is no problem, but value is an array,
json.stringify () has no way of identifying this type. So returned as the original value. Just like this.and the eval is not the same, he is through the way of JS to identify and convert, he can identify this type, it is easy to turn successfully;Now you can see why it's not recommended to use eval, because he doesn't recognize the standard JSON string, there may be other outlandish types, but in the end it turns you into a JSON object. but it can't be called.
※ The following is a summary of the three functions of the introduction and usage.
Json.stringify Json.parse also has an eval () function
Overview:
Json.stringify intelligently converts non-canonical JS objects, or JSON strings, to standard JSON strings
Json.parse the standard JSON string to the Jsonobject object. One1the embodiment of json.stringify intelligence
- The key name is not double quotation marks (including no quotation marks or single quotation marks), will automatically become double quotation marks, the string is single quotation marks, will automatically become double quotation marks
- The last property, followed by a comma, is automatically removed.
-
Span style= "font-family: Microsoft Jas Black" data-wiz-span= "Data-wiz-span" > This is a good understanding, that is, the non-array object in the final string does not guarantee the property order and the original consistent
-
What is yours new String (" Bala ")
"Bala"
new number"
2017
- Undefined, arbitrary functions (in fact, there is a function will happen magic, later said) and symbol value (symbol see ES6 for symbol)
- Occurs in a property value of a non-array object: is ignored during serialization
- When it appears in an array: is converted to null
JSON.stringify({x: undefined, y: function(){return 1;}, z: Symbol("")});//出现在非数组对象的属性值中被忽略:"{}"JSON.stringify([undefined, Object, Symbol("")]);//出现在数组对象的属性值中,变成null:"[null,null,null]"
- NaN, Infinity, and-infinity, both in arrays and non-array objects, are converted to null
- All attributes with the Symbol property key are completely ignored, even if the Replacer parameter is forced to specify that they are included
- Non-enumerable properties are ignored
2.
JSON.stringify(value[, replacer [, space]])
In fact, he has three parameters.
Typically only the first parameter, the JSON-formatted JS object, or the JS array are passed in.
- The second argument can be a function or an array
- The second item is a function, the format is this, function (key,value) {
}, each of the first parameters is processed. And each item must have a return value, otherwise it will be reported
undefined error
 
When the first parameter is Jsonobject, it passes in key and value, returns key after the logic processing, and the processed value;
when the first argument is Jsonarray, the Key+value is also passed in, then the key is the index of the array, and value is the data item.
when the second parameter is an array, the filter function is equivalent.
- When the first parameter is Jsonobject
only properties that appear in the array will be serialized into the result string key+value,
And this array exists but the source JS object does not exist in the property will be ignored, no error
- When the first parameter is Jsonarray
Method failed?? The third argument, with a feeling of chicken ribs: Specifies the padding character for indentation. Description Json.parse is to convert a standard JSON string to a JSON object.
It is worth noting that he has a second optional parameter.Perhaps you have found the clue that he is "Inside out" treatment. The eval () function is special, and the special point is that there is no way to understand the definition introduced in the official website. My understanding is: can use the JS syntax to parse into a JSON object.
<wiz_tmp_tag id= "Wiz-table-range-border" contenteditable= "false" style= "Display:none;" >
The conversion problem of JSON in JS