I am using ajax to get the data from the database. After getting data from table I am doing json_encode to get the data in json format. After that I am doing parseJSON to show the data in js.
when I am getting the data in json I just did
data = $.parseJSON(data);
console.log(data);
I got the data like this jQuery object.
From here I want to get the values of firstname.
I tried console.log(data.first_name);
but it not worked. It is showing undefined in the console tab. So can someone tell me how to get the first_name value here
Your data is array of objects and has data on indexes 0,1,2 so on so you need
try
console.log(data[0].first_name);
you can also loop through them
for(var a=0;a<data.length;a++) {
console.log(data[a].first_name);
}
You have been returned an array of objects. Iterate through all of them using:
for(var i = 0; i < data.length; i++) {
console.log(data[i].first_name);
}
jquery provide $.each() function to iterate object or array, see below sample code
data = $.parseJSON(data);
$.each(data, function(index, object){
console.log(object.first_name);
})
It looks like data is array of objects you have to iterate over this array and get the first_name attribute of each object in it as follows,
data = $.parseJSON(data);
data.forEach(function(item){
console.log(item.first_name);
})
Related
I have JSON data that needs to be pushed to a javascript array. I assume this is a regular list if it was Python. I can see the data being pushed in the array in console.log in this manner "[]". When I drill down this empty array, it shows the following data that I have actually pushed:
0:99
1:92
2:94
All this while, my length of list is zero. It isn't making sense to me at this point. Here is my code:
var l = [];
var m = [];
$(function() {
$.getJSON('anekal.json', function(data){
$.each(data.Percentage_Completed, function(i,f){
l.push(f);
});
$.each(data.Mentor_Name, function(i,f){
m.push(f);
});
});
});
console.log(l);
console.log(m);
How can I get the list of elements pushed from json dataset to javascript array?
Appreciate your time and help. Thank you!
The done method of getJSON will wait until the promise is complete, then add the data to your list.
$(function(){
var url = "anekal.json";
$.getJSON(url).done(function(data){
$.each(data, function(i,f){
l.push(f.Percentage_Completed);
m.push(f.Mentor_Name);
})
}); // end of getJSON
}); // end of function
EDIT: The problem happens because you iterate over each property of the data, not the data itself. Changing $.each(data.Percentage_Completed, function(i,f){ to $.each(data, function(i,f){ and then l.push(f); to l.push(f.Percentage_Completed); seems to fill up the array.
I need to store a lot of data in local storage for my app. I am successful in storing that. Now i need to fetch data in an array but of the selected key/values and not all or just 1 of them. I have named these key as "section"+id as seen below, need to select these in an array.
localStorage.getItem(key) will return the data for the key you provide. Data from localStorage is serialized so you need to parse it with JSON.parse(data) into a plain JS object and then you can work with it as any other object.
You can only get a single item with localStorage.getItem so if you want to get multiple items in an array you'll have to loop and get each one individually and put them in an array.
Maybe something like this:
var keys = Object.keys(localStorage).filter(function(key) {
return /^section\d+$/.test(key);
});
var dataArray = keys.map(function(key) {
return JSON.parse(localStorage.getItem(key));
});
First, we get all the "section###" keys with the filter function. Then we create a new array with the data for each corresponding item using the map function.
Try this code. I have edited it.
var keys = [];
var data = [];
for (var key in localStorage) {
if (key.indexOf("section") > -1)
keys.push(key);
}
for (var i in keys) {
data.push(localStorage.getItem(keys[i]))
}
My Existing Code and Explanation
Working Fine, But I want to Create a template like Its should read JSON Data that Having keys in any name, I am Always reading data from JSON object in 2nd Position
Now I read the JSON that passed in the argument (data) inside that using the for loop extract single object in group, after that get the data using key(Module_Name).. I know the key(Module_Name) is always present in second position only, Now its Working Fine with keyname rather i want to fetch the data using position, that also used as template in my other modules
/* render Screen Objects */
screenRenderer.displayScreens = function(data)
{
screenRenderer.renderLayout(function(cont, boo)
{
if (boo)
{
for(i=0;i<data.length;i++)
{
var buttons = "<button class='screen_button'>"+data[i].Module_Name+"</button>";
$("#screen-cont").append(buttons);
}
}
else
{
scr_cont.show();
}
})
}
This is My Requirement, kindly awaiting suggestions, answers..
Thanks in Advance
You can use the function Object.keys(obj) to get all keys of an object as array, which you can access with an Index and than use the String you get to access the property.
So it could look like this:
var obj = data[i];
var keys = Object.keys(obj);
var result = obj[keys[1]];
Hope this is what you want.
I have json returned from Database.I want to pick only one object Value and show it in the textbox. Here is my json.
[{
"ErrorMessage":"",
"ID":294,
"ExpenseID":0,
"EffectiveDate":"/Date(1262284200000)/",
"FormattedEffectiveDate":"01-01-2010",
"Perunit":null,
"VATRate":17.5,
"ChangedByID":1,
"ChangedByName":"superuser, superuser",
"Expense":null,
"ErrorSummary":null,
"ErrorList":[]
}]
I have Tried
var Jsoninvoice = JSON.stringify(data)
alert(Jsoninvoice.VATRate) and also alert(data.VATRate)
Thank you In advance.
You have an array containing 1 object. stringify turns this object into a string - you need it parsed so you can use it.
(I'm not sure if the object is parsed already, so to cover all bases, we'll parse it)
var Jsoninvoice = JSON.parse(data);
alert(Jsoninvoice[0].VATRate);
You have to specify the arrays index before you can access the properties.
It is already json object and stringify is not needed as #tymJV said you need to parse it if it is returned as string, just you need to access array item, as it is an array:
alert(data[0].VATRate)
SEE FIDDLE
You could use $.parseJSON(YOURJSON), and then use the keys to pull the data. Since it's in an array, you'll have to use [0] to pull the first item in the array (ie: your data).
Example
$(document).ready(function(){
var j ='[{"ErrorMessage":"","ID":294,"ExpenseID":0,"EffectiveDate":"/Date(1262284200000)/","FormattedEffectiveDate":"01-01-2010","Perunit":null,"VATRate":17.5,"ChangedByID":1,"ChangedByName":"superuser, superuser","Expense":null,"ErrorSummary":null,"ErrorList":[]}]';
var json = $.parseJSON(j);
alert("VATRate: "+json[0].VATRate);
});
Fiddle for reference
I have the following JSON value pushed from the server.
result=[{"id":1492,"name":"Delhi"},
{"id":109,"name":"Coimbatore"},
{"id":576,"name":"Konni"},
{"id":525,"name":"Kottayam"}
]
I know how to convert JSON Array to Javascript Array.Here is the code below(got from stackoverflow)
var locations = [];
$.each(result, function(i, obj) {
locations.push([obj.id,obj.name]);
});
I want to convert this JSON Array to a JavaScript Object Array so that I can access the values as jarray[0].id which will give me the value 1492. Please advice
You don't need to do anything to the result array. Just use result[0].id and it will evaluate to 1492.