Javascript 如何在console.log中打印json数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28149462/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:16:58  来源:igfitidea点击:

how to print json data in console.log

javascriptarraysjson

提问by Shojib Flamon

I cant access json data from javascript. Please help me how to access data from json data in javascript.

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

i have a json data like

我有一个 json 数据,比如

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

i have tried console.log(data) but log print object object

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

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

how to print console.log particular data? i need to print quantity-row_122 = 1 price-row_122 = 35.1

如何打印 console.log 特定数据?我需要打印quantity-row_122 = 1 price-row_122 = 35.1

回答by jdphenix

console.log(JSON.stringify(data))will do what you need. I'm assuming that you're using jQuery based on your code.

console.log(JSON.stringify(data))会做你需要的。我假设您正在根据您的代码使用 jQuery。

If you're wanting those two particular values, you can just access those and pass them to log.

如果您想要这两个特定值,您只需访问它们并将它们传递给log.

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

回答by akinjide

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

console.dir()will do what you need. It will give you a hierarchical structure of the data.

console.dir()会做你需要的。它将为您提供数据的层次结构。

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

like so

像这样

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

I don't think you need console.log(JSON.stringify(data)).

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

To get the data you can do this without stringify:

要获取数据,您无需执行此操作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 "

Note

笔记

The value from input_dataObject will be typeof "1": String, but you can convert to number(Int or Float)using ParseInt or ParseFloat, like so:

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

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

回答by itd

To output an object to the console, you have to stringify the object first:

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

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

回答by Naren Chejara

I used '%j' option in console.log to print JSON objects

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

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

回答by Naqeeb Sial

If you just want to print object then

如果您只想打印对象,则

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

If you want to access value of field in object then use

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

console.log(data.input_data);

回答by user11376402

Object

目的

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

input_data: 对象 price-row_122: " 35.1 " 数量-row_122: "1" 成功:true

回答by Corndog

This is an old post but I'm chiming in because (as Narem briefly mentioned) a few of the printf-like features are available with the console.logformatters. In the case of the question, you can benefit from the string, number or json formatters for your data.

这是一篇旧帖子,但我要补充一下,因为(正如 Narem 简要提到的)console.log格式化程序可以使用一些类似 printf 的功能。在这个问题的情况下,您可以从数据的字符串、数字或 json 格式化程序中受益。

Examples:

例子:

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