How To Get The Value Of Json Element Without The Double Quote
I generate json from datatable by this function: public string DataTableToJSONWithStringBuilder3(DataTable table) { var JSONString = new StringBuilder(); if
Solution 1:
You may use JSON.parse()
for parsing a JSON string.
vararray = JSON.parse(object.d);
You have not a valid JSON, because
- leading zeroes
- parenthesis
- function/method calls
The other solution, I do not recommend, is in this case
array = eval(object.d);
The better approach is to organize the data in the manner of not using eval()
.
Solution 2:
The only approach to work with this string is to use ReGex to remove what is out of the scope for a JSON object or array.
var str = '{' +
'"d": "[{' +
'"name": "Proyeksi Target",' +
'"data" : [' +
'[ Date.UTC(2016, 3, 01), 10.00 ],' +
'[ Date.UTC(2016, 1, 01), 5.00 ]' +
']' +
'}, {' +
'"name": "Realisasi",' +
'"data": [' +
'[Date.UTC(2016, 3, 01), 10.00 ],' +
'[Date.UTC(2016, 1, 01), 5.00]' +
']' +
'}]"' +
'}';
// fix syntax errors
str = str.match(/("\[\{".*}]")/g)[0];
str = str.substr(1, str.length - 2).replace(/(Date.UTC[^)]+\))/g, '"$1"');
// convert to objectvar jsonObj = JSON.parse(str);
var myStr = JSON.stringify(jsonObj, null, 2);
document.write(myStr);
Post a Comment for "How To Get The Value Of Json Element Without The Double Quote"