【问题标题】:How to print JSON data in console.log?如何在 console.log 中打印 JSON 数据?
【发布时间】:2015-03-24 19:51:04
【问题描述】:

我无法从 javascript 访问 JSON 数据。请帮助我如何在 javascript 中访问 JSON 数据中的数据。

我有一个类似的 JSON 数据

{"success":true,"input_data":{"quantity-row_122":"1","price-row_122":" 35.1 "}}

我尝试过 console.log(data) 但记录打印对象对象

success:function(data){
     console.log(data);
}

如何打印 console.log 特定数据? 我需要打印

quantity-row_122 = 1
price-row_122 = 35.1

【问题讨论】:

  • 试试console.log(JSON.stringify(data));
  • 使用 JSON.stringify(data) 将其转换为字符串 ang 然后尝试打印。
  • 1 分钟内有 4 个相同的答案,我想这个解决方案很明显:P

标签: javascript arrays json


【解决方案1】:

我通常是这样的:

console.log(JSON.stringify(data, undefined, 4));

【讨论】:

    【解决方案2】:

    你也可以使用util库:

    const util = require("util")
    
    > myObject = {1:2, 3:{5:{6:{7:8}}}}
    { '1': 2, '3': { '5': { '6': [Object] } } }
    
    > util.inspect(myObject, {showHidden: true, depth: null})
    "{\n  '1': 2,\n  '3': { '5': { '6': { '7': 8 } } }\n}"
    
    > JSON.stringify(myObject)
    '{"1":2,"3":{"5":{"6":{"7":8}}}}'
    

    原文来源:https://stackoverflow.com/a/10729284/8556340

    【讨论】:

      【解决方案3】:

      这是一篇旧帖子,但我正在插话,因为(正如 Narem 简要提到的)console.logformatters 提供了一些类似 printf 的功能。对于这个问题,您可以从数据的字符串、数字或 json 格式化程序中受益。

      示例:

      console.log("Quantity %s, Price: %d", data.quantity-row_122, data.price-row_122);
      console.log("Quantity and Price Data %j", data);
      

      【讨论】:

        【解决方案4】:

        对象

        输入数据:对象 price-row_122:“35.1” 数量-row_122:“1” 成功:是的

        【讨论】:

        • 几句话解释你的答案会很有帮助
        【解决方案5】:

        我在 console.log 中使用了 '%j' 选项来打印 JSON 对象

        console.log("%j", jsonObj);
        

        【讨论】:

          【解决方案6】:
          {"success":true,"input_data":{"quantity-row_122":"1","price-row_122":" 35.1 "}}
          

          console.dir() 将满足您的需求。它将为您提供数据的层次结构。

          success:function(data){
               console.dir(data);
          }
          

          像这样

          > Object
            > input_data: Object
                price-row_122: " 35.1 "
                quantity-row_122: "1"
              success: true
          

          我认为你不需要console.log(JSON.stringify(data))。

          要获取数据,您可以在没有stringify 的情况下执行此操作:

          console.log(data.success); // true
          console.log(data.input_data['quantity-row_122']) // "1"
          console.log(data.input_data['price-row_122']) // " 35.1 "
          

          注意

          input_data 对象的值将是typeof "1":String,但您可以使用 ParseInt 或 ParseFloat 转换为 number(Int or Float),如下所示:

           typeof parseFloat(data.input_data['price-row_122'], 10) // "number"
           parseFloat(data.input_data['price-row_122'], 10) // 35.1
          

          【讨论】:

            【解决方案7】:

            console.log(JSON.stringify(data)) 将满足您的需求。我假设您正在使用基于您的代码的 jQuery。

            如果您想要这两个特定值,您可以访问它们并将它们传递给log。

            console.log(data.input_data['quantity-row_122']); 
            console.log(data.input_data['price-row_122']); 
            

            【讨论】:

            • 这很好用,但 'quantity-row_122' 字段是动态生成的,它来自 html 表单。那么我们能做些什么呢?所以我想像数组一样打印
            • 需要引用这两个键名
            【解决方案8】:

            要将对象输出到控制台,必须先将对象字符串化:

            success:function(data){
                 console.log(JSON.stringify(data));
            }
            

            【讨论】:

              【解决方案9】:

              如果你只想打印对象,那么

              console.log(JSON.stringify(data)); //this will convert json to string;
              

              如果要访问对象中字段的值,请使用

              console.log(data.input_data);
              

              【讨论】:

                猜你喜欢
                • 2021-12-04
                • 2021-02-11
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2021-06-24
                • 1970-01-01
                相关资源
                最近更新 更多