Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
457 views
in Technique[技术] by (71.8m points)

javascript - 将JSON反序列化为JavaScript对象(Deserializing a JSON into a JavaScript object)

I have a string in a Java server application that is accessed using AJAX.(我在使用AJAX访问的Java服务器应用程序中有一个字符串。)

It looks something like the following:(它看起来像下面这样:)
var json = [{
    "adjacencies": [
        {
          "nodeTo": "graphnode2",
          "nodeFrom": "graphnode1",
          "data": {
            "$color": "#557EAA"
          }
        }
    ],
    "data": {
      "$color": "#EBB056",
      "$type": "triangle",
      "$dim": 9
    },
    "id": "graphnode1",
    "name": "graphnode1"
},{
    "adjacencies": [],
    "data": {
      "$color": "#EBB056",
      "$type": "triangle",
      "$dim": 9
    },
    "id": "graphnode2",
    "name": "graphnode2"
}];

When the string gets pulled from the server, is there an easy way to turn this into a living JavaScript object (or array)?(当字符串从服务器中拉出时,是否有一种简单的方法可以将其转换为活动的JavaScript对象(或数组)?)

Or do I have to manually split the string and build my object manually?(或者我是否必须手动拆分字符串并手动构建我的对象?)   ask by mj_ translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Modern browsers support JSON.parse() .(现代浏览器支持JSON.parse() 。)

var arr_from_json = JSON.parse( json_string );

In browsers that don't, you can include the json2 library .(在没有的浏览器中,您可以包含json2 。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...