Reputation: 2097
Say I have a JSON like this:
{
"99999":{
"success":true,
"data":{
"type":"blob",
"name":"random"
}
}
}
How do I access the "name", where the "99999" will be different each time?
Upvotes: 0
Views: 60
Reputation: 27192
Using JavaScript :
var jsonObj = {
"99999":{
"success":true,
"data":{
"type":"blob",
"name":"random"
}
}
};
var result = Object.keys(jsonObj).map(item => {return jsonObj[item].data.name});
console.log(result[0]);
Using jQuery :
var jsonObj = {
"99999":{
"success":true,
"data":{
"type":"blob",
"name":"random"
}
}
};
$.each(jsonObj, function(index, obj) {
console.log(obj.data.name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 0
Reputation: 517
you can use below code with JSON.parse.
var jsondata = '{"99999":{"success":true,"data":
{"type":"blob","name":"random"}}}';
$.each(JSON.parse(jsondata), function(idx, obj) {
console.log(obj.data.name);
});
Upvotes: 4
Reputation: 1970
Use this in your callback function (json is the json object not a string) :
function(json){
$.each(json,function(index,val){
var success=val.success;
var name=val.data.name;
//index = 99999 in your example
}
}
Upvotes: 0