我有一个从mysql表中检索的JSON编码对象,格式如下:
{immovable_property:
"{"immovable_subdivision":["a","b","c","d"],
"immovable_prop_details":["a","b ","c","d"],
"immovable_cost":["a","b","c","d"],
"immovable_present_value":["a","b","c","d"],
"immovable_owner":["a","b","c","d"],
"immovable_how_acquired":["a","b","c","d"],
"immovable_annual_income":["a","b","c","d"],
"immovable_remarks":["a","b","c","d"],
"add_subdivision":"e",
"add_prop_details":"e",
"add_cost":"e",
"add_present_value":"e",
"add_owner":"e",
"add_how_acquired":"e",
"add_annual_income":"e",
"add_remarks":"e "}"
}
我无法打印密钥的值,老实说,我不确定它是否是正确的JSON格式。任何帮助都非常感谢。
EDITED 我使用Laravel检索数据的代码如下:
$data = Property::where("users_id","=",Input::get('user_id'))->select('immovable_property')->first();
return Response::json($data);
我使用以下方式打印了返回的对象:
document.write(data.immovable_property);
,它给出了JSONLint验证的输出:
{
"immovable_subdivision": [
"a",
"b",
"c",
"d"
],
"immovable_prop_details": [
"a",
"b ",
"c",
"d"
],
"immovable_cost": [
"a",
"b",
"c",
"d"
],
"immovable_present_value": [
"a",
"b",
"c",
"d"
],
"immovable_owner": [
"a",
"b",
"c",
"d"
],
"immovable_how_acquired": [
"a",
"b",
"c",
"d"
],
"immovable_annual_income": [
"a",
"b",
"c",
"d"
],
"immovable_remarks": [
"a",
"b",
"c",
"d"
],
"add_subdivision": "e",
"add_prop_details": "e",
"add_cost": "e",
"add_present_value": "e",
"add_owner": "e",
"add_how_acquired": "e",
"add_annual_income": "e",
"add_remarks": "e "
}
答案 0 :(得分:3)
您的编辑完全改变了问题。
是的,JSON是有效的。您可以通过JSON.parse
解析它:
var obj = JSON.parse(immovable_property);
...然后访问结果上的命名属性,例如:
console.log(obj.immovable_subdivision.length); // 4
您可以通过Object.keys
获取所有属性名称的列表,该列表将返回一个数组。
示例:
// Your string
var immovable_property =
'{' +
' "immovable_subdivision": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_prop_details": [' +
' "a",' +
' "b ",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_cost": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_present_value": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_owner": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_how_acquired": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_annual_income": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "immovable_remarks": [' +
' "a",' +
' "b",' +
' "c",' +
' "d"' +
' ],' +
' "add_subdivision": "e",' +
' "add_prop_details": "e",' +
' "add_cost": "e",' +
' "add_present_value": "e",' +
' "add_owner": "e",' +
' "add_how_acquired": "e",' +
' "add_annual_income": "e",' +
' "add_remarks": "e "' +
'}';
// Parse it
var obj = JSON.parse(immovable_property);
// Access property via dot notation with a literal name
snippet.log('obj.immovable_subdivision.length = ' + obj.immovable_subdivision.length); // 4
// Or via brackets notation with a string namne
snippet.log('obj["immovable_subdivision"].length = ' + obj["immovable_subdivision"].length); // 4
// You can get all the keys (property names) as an
// array of strings from Object.keys, which lets you
// output them using bracketed notation
snippet.log("All properties:");
Object.keys(obj).forEach(function(key) {
snippet.log('obj["' + key + '"] = ' + obj[key]);
});

<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>
&#13;