来自Json Object的JSon字符串

时间:2011-10-04 10:27:45

标签: json json.net

如何从json对象中获取Json字符串? 我的要求就是这样,我将我的字符串解析为json对象并对json对象进行一些更改 我希望将更改重新添加到字符串

1 个答案:

答案 0 :(得分:0)

请从下面的链接下载JSON2.js并按照我编写的示例代码。

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

在项目中添加对该js文件的引用。

示例代码:

  var address = "India";
  var company = "mindfire";


  //JSON string
  var jsonStr = '{ '
              + '"Address" : '
              + '"' + address + '"'
              + ", "
              + '"Company" : '
              + '"' + company + '"'
              + ' }';

  var addressJson = JSON.parse(jsonStr); //Parse from json string to JSON object

  addressJson.Address = "Bhubaneswar Orissa"; //Change Data in JSON object
  addressJson.Company = "Mindfire Solutions";

  var updatedAddress = JSON.stringify(addressJson); //changes back in to the string from json object


  alert(updatedAddress);