【问题标题】:acessing JSON in JavaScript to get the value of a particular key在 JavaScript 中访问 JSON 以获取特定键的值
【发布时间】:2016-03-16 22:56:51
【问题描述】:
  • 我正在尝试在 JS 中获取密钥和 _html
  • 您能告诉我如何访问它吗?
  • 现在我只得到以下值

sportsTitle----->

[{
    "type": "p",
    "key": "100",
    "ref": null,
    "props": {
        "dangerouslySetInnerHTML" : {
            "__html": "hello world"
        }
    },
    "_owner": null,
    "_store": {}
}]

在下面提供我的代码

let sportsTitle = renderedElement.props.children;
console.log("sportsTitle----->" + JSON.stringify(sportsTitle));

JSON 结构 渲染元素----->

{
    "type": "div",
    "key": null,
    "ref": null,
    "props": {
        "className": "sports-page",
        "children": [
            {
                "type": "p",
                "key": "100",
                "ref": null,
                "props": {
                    "dangerouslySetInnerHTML": {
                        "__html": "hello world"
                    }
                },
                "_owner": null,
                "_store": {}
            }, 
            {
                "type": "p",
                "key": "200",
                "ref": null,
                "props": {
                    "dangerouslySetInnerHTML": {
                    "__html": "hello world2"
                }
            },
            "_owner": null,
            "_store": {}
        }]
    },
    "_owner": null,
    "_store": {}
}

【问题讨论】:

标签: javascript jquery html backbone.js reactjs


【解决方案1】:

因为renderElement.props.children 是一个数组,所以你需要知道你想要触摸的索引,比如renderElement.props.children[0].props.dangerouslySetInnerHTML.__html。

【讨论】:

  • 感谢您的解释..我不知道数组访问...所有这些天...但下面的代码只工作 enderedElement.props.children[0].props.dangerouslySetInnerHTML.__html;跨度>
【解决方案2】:

看起来像一个疯狂的 JSON 结构,但试试renderedElement.keyrenderedElement.props.children[props].dangerouslySetInnerHTML.__html

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    相关资源
    最近更新 更多