javascript - 如何选择 key : value pair in string format used in a JSON object?

标签 javascript json object key

我想选择长度,但它是字符串形式。类似 attribs.length 的东西,但什么也没返回。

attribs: {
    "length": ""4"",
    "nightride": "null"
}

我尝试过拆分它并重新加入它,但无论哪种方式我都没有得到我想要的。 我也尝试过 attribs[length] 和 attribs["length"] 但无济于事。

这是它的主要 block :

{
    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"] }}

最佳答案

如果 attribs 是一个实际的 javascript 对象并且您想要 attribs.length 的数值

var x = parseFloat(attribs.length.replace(/"/g,''));

关于javascript - 如何选择 key : value pair in string format used in a JSON object?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33139498/

相关文章:

javascript - Phaser.js : cannot read property '0' on tiled map layer

java - 其余 Jersey json 编码

java - 扩展对象隐式

javascript - JSLint : 'numeral' was used before defined?

javascript - React 函数组件 setTimeout - 多次渲染调用和重新渲染(推荐方法)(多次触发)

c# - 将 IConfigurationSection 绑定(bind)到没有 ASP.NET Core 的复杂对象

javascript - 指向 JSON blob 中的不同值以放入数组

javascript - 我们可以通过 Graph API 发送 "Notifications App"吗?

javascript - 是否可以在 CSS 动画中调用特定的关键帧?

javascript - 对象或数组或两者