【问题标题】:how to print heading of json in javascript?如何在javascript中打印json的标题?
【发布时间】:2021-10-11 19:22:09
【问题描述】:

我知道这是一个非常简单的问题,但我是 json 的初学者。谁能回答如何在控制台中打印标题。

这是我的代码:

  var jsonstr = '{"profile":{"name" : "raj","age":"35"}}' ;
  var json = JSON.parse(jsonstr);
  // here I want to print console.log("profile")

现在我想在控制台中打印“个人资料”。

【问题讨论】:

    标签: javascript node.js arrays json printing


    【解决方案1】:

    您可以尝试Object.keys() 索引为 0:

    var jsonstr = '{"profile":{"name" : "raj","age":"35"}}' ;
    var json = JSON.parse(jsonstr);
    console.log(Object.keys(json)[0]);

    【讨论】:

    • 你能告诉我如何在变量中获得像{"name" : "raj","age":"35"} 这样的剩余值吗? @马蒙
    • @Siddharth,像Object.values(json)[0]一样使用.values()
    猜你喜欢
    • 2011-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-08
    • 1970-01-01
    • 2012-12-22
    • 2011-08-03
    相关资源
    最近更新 更多