Get values from single json array [duplicate] - javascript

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

Related

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.

How to access object of an object returned by json [duplicate]

This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 5 years ago.
in a little laravel application I'm working on I'm returning data from an ajax request like this:
return response ()->json ( $subject::where('id', $subject->id)->with('division')->get(['id', 'name']));
This returned something quite like an object that have nested objects. This how my results looks when I log it to the console.
I want to get the name and id of the subject details returned, which in this case is History and 8. Also I want to be able to access the division array and properties of the object it has.
I do this to log the name of the subject console.log(data.name) but in returned I get:
undefined
How can I achieve this?
You have an array of objects, which has one property (division) - which contains another array. So you have to access the array indices
console.log(data[0].division[0].name);
Looking at the object structure we see that this is an array of elements, so it should be data[0].name to fetch History text
I'm assuming data is your entire object.
Try data[0].name

AngularJS: Get value from JSON data [duplicate]

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() {
});

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

Quotes are escaped when passing JSON as a POST variable [duplicate]

This question already has answers here:
Why are $_POST variables getting escaped in PHP?
(6 answers)
Closed 9 years ago.
I'm doing an Ajax call to my server and need to send an array. I'm encoding the array using JSON. That results in this data sent to the server using a POST request:
selection=%5B%221%22%5D
On the server, I have this code:
echo urldecode($_REQUEST['selection']);
This results in:
[\"1\"]
Note that there are no backslashes in the request. I checked that with Firefox's dev tools.
Where were the backslashes added? Am I doing something wrong here? I can't decode the string like this.
This is the client-side code:
$.ajax({
type: "POST",
url: "<my-uri>/rule/add.php",
data: {
selection: JSON.stringify(["1"]) // in reality this is a variable array
}
}).done(function(data){
alert(data);
});
This happens because your server is configured to add slashes to quotes.
If you wish to avoid this go to your php.ini and set magic_quotes_gpc to 0.

Categories

Resources