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

时间:2021-06-03 16:58:34

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(数据)但是记录了打印对象对象

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特定数据?我需要打印数量-row_122 = 1 price-row_122 = 35.1

5 个解决方案

#1


19  

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.

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

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

#2


10  

{"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_data Object will be typeof "1": String, but you can convert to number(Int or Float) using ParseInt or ParseFloat, like so:

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

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

#3


7  

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

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

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

#4


1  

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);

#5


1  

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

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

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

#1


19  

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.

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

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

#2


10  

{"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_data Object will be typeof "1": String, but you can convert to number(Int or Float) using ParseInt or ParseFloat, like so:

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

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

#3


7  

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

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

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

#4


1  

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);

#5


1  

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

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

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