I'm trying to retrieve the JSON data given below but I'm unable to.
Since I'm using Javascript Ajax success function, when I try doing alerts with the code,
$.ajax({
type:'GET',
url:myURL,
success : function(data) {
alert(data);
//{"object1":{"mainIsActive":"A","mainBuildingGL":"01493","mainIsUnderCons":"B"},"object2":[[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],{"newBuildingGL":"15450"}]}
}
});
I am retrieving the below JSON data.
{"object1":{"mainIsActive":"A","mainBuildingGL":"01493","mainIsUnderCons":"B"},"object2":[[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],{"newBuildingGL":"15450"}]}
But when I try trying to get the value of mainIsActive using:
alert(data.object1.mainIsActive);
I am getting the error in the console:
"Cannot read property 'mainIsActive' of undefined at Object.success (:143:30)"
Can you please help? I also attached the JSON image so you can understand the structure better.
The JSON data will be available in object structure once you have parsed it using
JSON.parse(StringYouWantToParse)
This code seems to work properly:
var x = '{"object1":{"mainIsActive":"A","mainBuildingGL":"01493","mainIsUnderCons":"B"},"object2":[[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],[{"statLabel":"Cafeteria","statCount":"1"},{"statLabel":"Restaurant","statCount":"2"}],{"newBuildingGL":"15450"}]}';
var data = JSON.parse(x);
alert(data.object1.mainIsActive);
Related
I have array in my php file:
$invoices_arr = [];
foreach ($invoices as $key=>$invoice){
$invoices_arr[$key]['id']=$invoice->get_id();
$invoices_arr[$key]['code_text']=$invoice->get_code_text();
$invoices_arr[$key]['invoice_name']=$invoice->get_invoice_name();
$invoices_arr[$key]['status_invoice']=$invoice->get_status_invoice();
$invoices_arr[$key]['attachments']=$invoice->get_attachments();
}
echo json_encode(['invoices'=>$invoices_arr]);
My ajax call:
$.ajax({
data:{lead_id:$("#lead_id").val()},
url: sJSUrlGetAllLeadInvoices,
success: function (data) {
var obj = jQuery.parseJSON(data);
$("#all_invoice_table tbody").empty();
$(obj.invoices).each(function(key,value){
$('#all_invoice_table').append('<tr><td>'+value.invoice_name+'</td><td class="invoice_status">'+value.status_invoice+'<td>attt</td><td><button id="'+value.id+'" class="edit_invoice_'+value.id+'">Edit invoice</button</td><td><button class="send_invoice_btn_'+value.id+'">Send invoice</button></td><td><img class="delete_invoice_'+value.id+'" src="/images/icons/delete.gif"></td><td class="invoice_hidden_column_'+value.id+'">'+value.code_text+'</td></tr>');
});
}
});
In field value.attachments there is php serialize array, for example, string "a:1:{i:0;s:36:"../../pdf_invoices/2017-10-10015.pdf";}" .
How can I convert this string to array in js?
I try do it:
var mas=JSON.parse(value.attachments);
But I get error SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
How can I solve it? Thanks for any help.
Maybe you should unserialize inside PHP code before encode in json
You don't need to parse the result with jQuery.parseJSON because jQuery do it for. If you inspect data using console.log() you will see that is a Javascript object ready for to use.
You need to pass datatype as json.
you can use jQuery parse method for parsing json response.
datatype:json
I have the following json data that looks like this...
What I am trying to do is store the temp numbers into a javascript array to display on a graph. This is my code that I was working on...
$.ajax({
type: 'GET',
url: "http://api.openweathermap.org/data/2.5/forecast?q=Redlands,us&mode=json&units=imperial&cnt=20&APPID=5eea63b2ee3505c58713d9149832d4c5",
dataType: 'json',
success: function(data){
//console.log(data.list[0].main.temp);
//trying to parse through data for graph
var date = [];
$.each(data,function(index, data){
date.append(data.list[index].main.temp);
});
}
});
But it is not working I get the following error...
Uncaught TypeError: Cannot read property 'city' of undefined
Anyone know how I can fix this?
i see working , same from Erik Engervall, i change var data for current
$.each(data.list,function(index, current){
date.push(current.main.temp);
});
It seems you're iterating over the entire data object, whereas I believe you only wish to iterate over the data.list:
$.each(data.list, function(index, val) {
date.push(val.main.temp);
});
Also, JavaScript arrays uses push.
Updated answer after Mikel.
You need to use JSON.parse().
var dataList = JSON.parse(data).list;
please help, i know its something easy but cant figure it out.
I have a Json Response that comes back looking like this
onSmData({"valid":true,"token":"201777121"});
on my javascript i try to read the response like this
console.log(data[0].valid);
console.log(data[1].token);
but i keep getting this error.
Uncaught TypeError: Cannot read property 'valid' of undefined
What am I doing wrong?
Try data.token and data.valid?
I believe first of all you need to parse the json value. For example:
var jsonObject = JSON.parse(response);
and only then try to get the values like you do from the parsed object - this example case jsonObject.
The Following example will make it clear:
var jsontext = '{"name":"x","age":"11"}';
var getContact = JSON.parse(jsontext);
document.write(getContact.name + ", " + getContact.age);
// Output: x, 11
Data doesn't seems to be an Array. I guess this should work :
console.log(data.valid);
console.log(data.token);
To be sure, juste log :
console.log(data);
Please have a look. I am getting an error of "Uncaught type error: can not read the property 'title'".
here is how code looks like :
db.transaction(function(transaction) {
transaction.executeSql('INSERT INTO post_details(post_title, post_date,post_comment,post_content,post_categories,post_image) VALUES ("'+data.posts[i].title+'","'+data.posts[i].date+'","'+data.posts[i].comment_count+'","'+data.posts[i].content+'","'+data.posts[i].categories+'","'+data.posts[i].thumbnail_images.full.url+'")',[], nullHandler,errorHandler);
});
This "data" is a variable holding JSON data which i am calling through AJAX. Which is returning properly if am normally alert it.
Thank you!
I'm creating a D3 bubble chart, using json data. I'm using sinatra for my app, and my json data is available at localhost:4567/data.json
I tried using
var myData = [];
$.get('data.json', function(data) {
myData = data;
console.log(myData);
.......
and I get the correct values in the javascript console, but the bubble chart does not render. (The rest of the code works if I copy and paste the data from 'data.json' and set it to a var, but it does not work if I use the $get method).
Do you have any ideas on how I could access this json data from localhost:4567?
Much appreciated,
Tim
I think what is probably going on is that jquery isn't automatically parsing the data as a JSON object due to missing MIME headers in the response from your server. Try using getJSON instead.
you can simply use
d3.json('data.json', function(data) {
myData = data;
console.log(myData);
.......
to read the json file