admin管理员组文章数量:1134243
I cant access JSON data from javascript. Please help me how to access data from JSON data in javascript.
i have a JSON data like
{"success":true,"input_data":{"quantity-row_122":"1","price-row_122":" 35.1 "}}
i have tried console.log(data) but log print object object
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
I cant access JSON data from javascript. Please help me how to access data from JSON data in javascript.
i have a JSON data like
{"success":true,"input_data":{"quantity-row_122":"1","price-row_122":" 35.1 "}}
i have tried console.log(data) but log print object object
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
Share
Improve this question
edited Apr 1, 2021 at 16:42
Syscall
19.8k10 gold badges43 silver badges57 bronze badges
asked Jan 26, 2015 at 11:44
Shojib FlamonShojib Flamon
1,5093 gold badges13 silver badges18 bronze badges
4
|
9 Answers
Reset to default 99console.log(JSON.stringify(data))
will do what you need. I'm assuming that you're using jQuery based on your code.
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']);
I used '%j' option in console.log to print JSON objects
console.log("%j", jsonObj);
To output an object to the console, you have to stringify the object first:
success:function(data){
console.log(JSON.stringify(data));
}
I usually do like this:
console.log(JSON.stringify(data, undefined, 4));
{"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.
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))
.
To get the data you can do this without 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:
typeof parseFloat(data.input_data['price-row_122'], 10) // "number"
parseFloat(data.input_data['price-row_122'], 10) // 35.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);
You can also use util
library:
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}}}}'
original source : https://stackoverflow.com/a/10729284/8556340
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.log
formatters. In the case of the question, you can benefit from the string, number or json formatters for your data.
Examples:
console.log("Quantity %s, Price: %d", data.quantity-row_122, data.price-row_122);
console.log("Quantity and Price Data %j", data);
Object
input_data: Object price-row_122: " 35.1 " quantity-row_122: "1" success: true
本文标签: javascriptHow to print JSON data in consolelogStack Overflow
版权声明:本文标题:javascript - How to print JSON data in console.log? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736803885a1953605.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
console.log(JSON.stringify(data));
– chridam Commented Jan 26, 2015 at 11:47console.log(JSON.stringify(data));
is the simplest answer, but if you want something more fancy you can useconsole-log-json
from the NPM repository: npmjs.com/package/console-log-json – 7wp Commented Jul 13, 2022 at 15:24