json 与 string 互转

时间:2023-03-09 21:23:45
json 与 string 互转

1. json字符串转换成json对象

js:

JSON.parse('{"age":"28"}')

eval('(' + '{"age":"28"}' + ')')

jq:

$.parseJSON('{"age":"28"}')

2.json对象转换成json字符串

js:

JSON.stringify({age: "28"})

jq:

$.stringify({age: "28"})

3.低版本ie

引入js

https://github.com/douglascrockford/JSON-js

4. 美文欣赏

你所不知道的JSON.parse() 和 JSON.stringify() – 高级用法

5. 获取json文件常用方式

    $.ajax({
type: "GET",
url: "../Data/pickConditions.json",
dataType: "json",
onComplete: function (data) {
console.log("hello", data);
}
});
$.getJSON('../Data/pickConditions.json', function (data) {
console.log(data);
});
$.get('../Data/pickConditions.json').done(function (data) {
console.log(data);
});