简体   繁体   English

javascript将字符串转换为数据

[英]javascript convert string to data

I have JavaScript code below. 我下面有JavaScript代码。

var data = "[{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}]";

I need convert the string to an data by delete the "" surrounding the array stored as "data" in JavaScript so after convert it suppose like below: 我需要的字符串转换为data的删除""周围的存储阵列"data"在JavaScript等以后把它转换假设象下面这样:

var data = [{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}];

How to make the convert? 如何进行转换?

To convert a JSON object to Javascript object use: 要将JSON对象转换为Javascript对象,请使用:

var data = '[{"PR_ID":23096,"P_ID":23014},{"PR_ID":33232,"P_ID":23014},{"PR_ID":33308,"P_ID":23014},{"PR_ID":33309,"P_ID":23014}]';
JSON.parse(data);

But first change the double quote to single quote, otherwise the JSON object wont be a valid JSON. 但首先将双引号更改为单引号,否则JSON对象将不是有效的JSON。

After this you can walk the array in the following way: 之后,您可以按照以下方式遍历数组:

var jsonParsed = JSON.parse(data);

for(var val in jsonParsed) {
   if(jsonParsed.hasOwnProperty(val)) {
      // do something with the values
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在javascript中将字符串数据转换为Geojson - How to convert a string data to Geojson in javascript JavaScript中如何将16进制的字符串数据转为ArrayBuffer - How to convert a hexadecimal string of data to an ArrayBuffer in JavaScript 文件上传将字符串数据转换为json数据javascript - file upload convert string data to json data javascript 如何将字符串数据从ajax成功转换为javascript数据? - How to Convert string data from ajax success to javascript data? Javascript将JSONP数据从字符串转换为Json对象 - Javascript Convert JSONP data from string to Json object 将包含字符串数据的变量转换为JavaScript REST API的JSON对象 - Convert variable that contain string data into JSON Object for JavaScript REST API 如何在不同时区将日期字符串转换为 javascript 中的数据? - How can I convert date string to data in javascript in different timezone? 将字符串转换为 JSON 或 Javascript/React 中的其他数据结构 - Convert string into JSON or other data structure in Javascript/React 将servelt中的字符串转成json数据传给javascript:ajax - convert the string in servelt to json data and pass it to javascript : ajax Javascript HTML5如何将二进制数据转换为字符串 - Javascript html5 how to convert binary data into string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM