如何 select 键:JSON 对象中使用的字符串格式的值对?
How to select a key: value pair in string format used in a JSON object?
我想要 select 长度,但它是字符串形式。类似 attribs.length 的内容,但未返回任何内容。
attribs: {
"length": ""4"",
"nightride": "null"
}
我试过拆分它并重新加入它,但无论哪种方式我都没有得到我想要的。
我也试过 attribs[length] 和 attribs["length"] 但无济于事。
这是它来自的主要块:
{
places: [
{
city: "Boise",
state: "Idaho",
country: "United States",
name: "Elk Meadows",
parent_id: null,
unique_id: 1637,
directions: ""From Boise's north end (at the intersection of Hill and Bogus Basin roads), travel north up Bogus Basin road about 16.5 miles.the pavement ends at lower (main)parking lot near Bogus Creek Lodge. Proceed on the Boise Ridge Rd. Where the sandy surface turns to pavement as it leavesthe main ridge rd.and splitsright, up through a series of switch backs to the Pioneer Lodge milage starts here",
lat: 43.764,
lon: -116.10324,
description: null,
date_created: null,
children: [ ],
activities: [{
name: "Elk Meadows",
unique_id: "1-118",
place_id: 1637,
activity_type_id: 5,
activity_type_name: "mountain biking",
url: "http://www.singletracks.com/item.php?c=1&i=118",
attribs: {
"length": ""4"",
"nightride": "null"
},
description: "ride east up behind Pioneer Lodge past the tennis courts on your left. the double track becomes more defined",
length: 4,
activity_type: {
created_at: "2012-08-15T16:12:35Z",
id: 5,
name: "mountain biking",
updated_at: "2012-08-15T16:12:35Z"
},
thumbnail: "http://images.singletracks.com/2010/11/Elk-Meadows-on-the-Ranier-0.jpg",
rank: null,
rating: 3
}
]
},
这是我 angular 获取数据的尝试:
{{ result.activities[0].attribs["length"] }}
由于 length
属性 周围有一组额外的引号,您可以使用 JSON.parse
将其解析为字符串。
JSON.parse(obj.attribs.length);
要将其转换为数字,请使用 parseInt()
。所以这一切变成了:
var length = parseInt(JSON.parse(obj.attribs.length), 10);
您有一个无效 JSON。 JSON 应始终是单个 array 或单个 object.
这是正确的 JSON:
{
"attribs": {
"length": "4",
"nightride": "null"
}
因此,您可以:
写信给您的 API 提供商的支持或停止使用此 API。在 99% 的情况下,这是一个正确的解决方案。
做一些非常糟糕的黑客攻击。无论如何我都不建议这样做。但是,这可以解决一个问题。就像这样:
.done(function(data) {
data = "{" + data.replace("\"\"", "\"") + "}";
var obj = JSON.parse(data);
alert(obj.attribs.length);
});
如果 attribs 是一个实际的 javascript 对象并且您想要 attribs.length
的数值
var x = parseFloat(attribs.length.replace(/"/g,''));
大家好!我很尴尬之前没有看到这个,但是有两个 key:value 对在我看的对象响应的下方,
attribs: {
"length": ""4"",
"nightride": "null"
},
我们都知道并喜爱标准值格式中的另一个 key:value 对距离:
attribs: {
"length": ""4"",
"nightride": "null"
},
description: "ride east up behind Pioneer Lodge past the tennis courts on your left. the double track becomes more defined",
length: 4,
强调
length: 4,
所以当然,我将我的符号指向了这一点。
Jaromanda X 的回答最直接,在我看来,clean/precise/accurate 回答,所以他接受了检查。
我想要 select 长度,但它是字符串形式。类似 attribs.length 的内容,但未返回任何内容。
attribs: {
"length": ""4"",
"nightride": "null"
}
我试过拆分它并重新加入它,但无论哪种方式我都没有得到我想要的。 我也试过 attribs[length] 和 attribs["length"] 但无济于事。
这是它来自的主要块:
{
places: [
{
city: "Boise",
state: "Idaho",
country: "United States",
name: "Elk Meadows",
parent_id: null,
unique_id: 1637,
directions: ""From Boise's north end (at the intersection of Hill and Bogus Basin roads), travel north up Bogus Basin road about 16.5 miles.the pavement ends at lower (main)parking lot near Bogus Creek Lodge. Proceed on the Boise Ridge Rd. Where the sandy surface turns to pavement as it leavesthe main ridge rd.and splitsright, up through a series of switch backs to the Pioneer Lodge milage starts here",
lat: 43.764,
lon: -116.10324,
description: null,
date_created: null,
children: [ ],
activities: [{
name: "Elk Meadows",
unique_id: "1-118",
place_id: 1637,
activity_type_id: 5,
activity_type_name: "mountain biking",
url: "http://www.singletracks.com/item.php?c=1&i=118",
attribs: {
"length": ""4"",
"nightride": "null"
},
description: "ride east up behind Pioneer Lodge past the tennis courts on your left. the double track becomes more defined",
length: 4,
activity_type: {
created_at: "2012-08-15T16:12:35Z",
id: 5,
name: "mountain biking",
updated_at: "2012-08-15T16:12:35Z"
},
thumbnail: "http://images.singletracks.com/2010/11/Elk-Meadows-on-the-Ranier-0.jpg",
rank: null,
rating: 3
}
]
},
这是我 angular 获取数据的尝试:
{{ result.activities[0].attribs["length"] }}
由于 length
属性 周围有一组额外的引号,您可以使用 JSON.parse
将其解析为字符串。
JSON.parse(obj.attribs.length);
要将其转换为数字,请使用 parseInt()
。所以这一切变成了:
var length = parseInt(JSON.parse(obj.attribs.length), 10);
您有一个无效 JSON。 JSON 应始终是单个 array 或单个 object.
这是正确的 JSON:
{
"attribs": {
"length": "4",
"nightride": "null"
}
因此,您可以:
写信给您的 API 提供商的支持或停止使用此 API。在 99% 的情况下,这是一个正确的解决方案。
做一些非常糟糕的黑客攻击。无论如何我都不建议这样做。但是,这可以解决一个问题。就像这样:
.done(function(data) { data = "{" + data.replace("\"\"", "\"") + "}"; var obj = JSON.parse(data); alert(obj.attribs.length); });
如果 attribs 是一个实际的 javascript 对象并且您想要 attribs.length
的数值var x = parseFloat(attribs.length.replace(/"/g,''));
大家好!我很尴尬之前没有看到这个,但是有两个 key:value 对在我看的对象响应的下方,
attribs: {
"length": ""4"",
"nightride": "null"
},
我们都知道并喜爱标准值格式中的另一个 key:value 对距离:
attribs: {
"length": ""4"",
"nightride": "null"
},
description: "ride east up behind Pioneer Lodge past the tennis courts on your left. the double track becomes more defined",
length: 4,
强调
length: 4,
所以当然,我将我的符号指向了这一点。
Jaromanda X 的回答最直接,在我看来,clean/precise/accurate 回答,所以他接受了检查。