AngularJS: Get value from JSON data [duplicate] - javascript

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 7 years ago.
I'm trying to get value from response JSON data. How can i get the values domain, status, key.
Response JSON data:
{"example.com":{"status":"YES","key":"example"}}
Angular JS Code
$http.post('http://exampleURL.com', postData).success(function(response){
console.log(response);
alert();
}).error(function() {
});

use angular.fromJson to parse JSON then traverse it using .
$http.post('http://exampleURL.com', postData).success(function(response){
console.log(response);
var data = angular.fromJson(response);
console.log(data['example.com'].status);
}).error(function() {
});

Related

Read nested value from json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 3 years ago.
On the image You can see what I want to read
console.log("exact val = " + json._array.loftybot.spells.name)
and it gives [TypeError: Cannot read property 'spells' of undefined]
You can see in terminal structure of json
How to get into these values?
I want save values from json to array const json = new ObservableArray
And then use all of these values in my program
How about json._array[0].loftybot.spells.name? with an index 0 on your _array and go further on nested json. SEE at MDN

How to get data from Json Object using javascript [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 4 years ago.
I'm getting data as a json object
var data = JSON.parse(JSON.stringify(data));
console.log(data);
On data console I got this. I want to get key and its value .
{lobby: {…}}
lobby:
8jmb9ca3s04c8el4j5sf0d:"AD8BJBkMKCBoYg_qAAAB"
38cjllj78cx0lic58ujxou:"PX51X9z_M34_9BvtAAAD"
ba8gs1y8779kmakdapxk1:"UowsBDCCsZSpojzPAAAA"
Parse JSON to Object var data = JSON.parse(jsonData);
Use Object.keys(data); to get all keys
Use data.key or data['key'] to get key value.

Get values from single json array [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 8 years ago.
Here is the json returning from controller through ajax
[{"UserID":"1","Username":"admin","Password":"#######","FullName":"Hassan","Email":"admin#admin.com","Phone":null,"Mobile":null,"PreviousAddress":null,"CNIC":null,"theme":null,"GroupID":"1"}]
i want to get values from the array.
I tried like this
success: function(data){
console.log(data.UserID);
}
but i get undefined in the console.
You need to access the first index of the array
console.log(data[0].UserID);
Set dataType to json in ajax call. The result will be parsed and returned as an object in your success result.
$.ajax({
url: "yourscript.php",
type: "POST",
data: your data,
dataType: "json"
}).
success(function (data) {
...
});
In Javascript [] means array therefore your Object it is actually an array of objects [{"UserID":"1",...},{"UserID":"2",...}, etc]
In order to access each object you will need to loop trough or use the index number you want to access like var obj =[{"UserID":"1",... then obj[0].UserID

using d3.json method with Json object [duplicate]

This question already has answers here:
d3 js - loading json without a http get
(3 answers)
Closed 8 years ago.
I am generating an zoomable chart by using d3.js.
chart data is being received as json object. how to change the following code to work with JSON object
d3.json("mydata.json", function(json) {
//TODO:
});
instead of file name "msdata.json" I want to use Json object. please help me how to do this.
If you already have a JSON string (there's no such thing as JSON object) in the script, you can remove your d3.json request and use JSON.parse to convert the JSON string to an object (usually it's an array) like so:
var data = JSON.parse(yourJSONString);
Then you can use it to compute data joins or whatever you want to do with it in d3:
someSelection.data(data).enter()... // etc.

Accessing python dictionary in javascript [duplicate]

This question already has answers here:
Parse JSON in JavaScript? [duplicate]
(16 answers)
Closed 8 years ago.
I am sending dictionary from python like this:
{'root':['value','path','type'],.......}
i am send it to javascript by ajax call request by serializing it,
How to access that dictionary in javascript.
thanks in Advance
Suppose you are making AJAX call in below way, you will get the response dict as resValue. use JSON.parse method on it
$.getJSON( "/url", {params }, function( data, status, xhr ) {
$.each(data.response, function(resKey, resValue){
if(resKey == "success"){
var _result = JSON.parse(resValue);
}
}
}

Categories

Resources