Javascript json object parsing [duplicate] - javascript
This question already has answers here:
How can I access and process nested objects, arrays, or JSON?
(31 answers)
Closed 9 years ago.
$(document).ready(function () {
var oOpenOrders = new Array();
$('#btn').click(function () {
$.ajax({
type: 'GET',
url: 'http://192.168.19.22/test.php',
contentType: "application/json; charset=utf-8",
dataType: 'json',
success: function (data) {
debugger;
for(var i=0; i<data.length; i++){
alert(typeof(data.id));
oOpenOrders.push(data);
}
var obj = jQuery.parseJSON(data);
debugger;
this code snippet i am using and want to parse it(want to use lat, long) even data.length say correct length 4 for following json, following json is coming from ajax call.:
[{"id":"3037","latitude":"28.617422000","longitude":"77.381180000"},
{"id":"3036","latitude":"28.617422000","longitude":"77.381180000"},
{"id":"3035","latitude":"28.617422000","longitude":"77.381180000"},
{"id":"3034","latitude":"28.617422000","longitude":"77.381180000"}]
any idea ?
Try this
alert(typeof data[i].id);
Related
Can't pass array through ajax [duplicate]
This question already has answers here: Javascript POST not working - Sending Javascript array to PHP (4 answers) Closed 4 years ago. Trying to have data passed to a php script so the said data can be added to the session. The debug console log returns are the following: the quant array is correct and typeof is object, the JSON.stringified data's type is string , lastly success from the ajax success. In the PHP script var_dump return is NULL $('#bigsubmit').click(function() { var quant = []; $('.input-number').each(function() { var tmp = {}; tmp.id = this.id; tmp.qu = $(this).val(); quant.push(tmp); }); console.log(quant); var data = JSON.stringify(quant); console.log(typeof(data)); $.ajax({ type: "POST", url: url, data: { data: data }, success: function() { console.log("success"); } }); the php script (url var) <?php session_start(); $_SESSION['test'] = $_POST['data']; var_dump($_SESSION['test']); ?>
Your success callback function isn't taking in a parameter, try changing to this, success:function(data) { console.log(data); }
Display returned JSON [duplicate]
This question already has answers here: How can I access and process nested objects, arrays, or JSON? (31 answers) Closed 6 years ago. Im having an issue being able to access the returned object and getting it to display. It is cross domain so I am using jsonp, but can't seem to access the returned object and I don't know why. $(function(){ var API = "https://ratesjson.fxcm.com/DataDisplayer"; $.ajax({ url: API, dataType: 'jsonp', crossDomain: true, success:function( msg ) { var Symbol = msg.Rates.Symbol; console.log("Symbol: " +Symbol); } }); }); The data looks like this, and I have no control over how it is formatted or returned: jQuery1102016139126126654446_1458048453204({"Rates":[{"Symbol":"EURAUD","Bid":"1.48741","Ask":"1.48751","Spread":"1.00","ProductType":"1",},{"Symbol":"Copper","Bid":"2.23","Ask":"2.2325","Spread":"2.50","ProductType":"2",},{"Symbol":"AUDNZD","Bid":"1.12279","Ask":"1.12295","Spread":"1.60","ProductType":"1",},{"Symbol":"EURSEK","Bid":"9.2382","Ask":"9.23974","Spread":"15.40","ProductType":"1",},{"Symbol":"CADJPY","Bid":"84.603","Ask":"84.609","Spread":"0.60","ProductType":"1",},{"Symbol":"USDCHF","Bid":"0.98628","Ask":"0.98632","Spread":"0.40","ProductType":"1",},{"Symbol":"USDCNH","Bid":"6.50553","Ask":"6.50581","Spread":"2.80","ProductType":"1",},{"Symbol":"XAGUSD","Bid":"15.222","Ask":"15.263","Spread":"4.10","ProductType":"5",},{"Symbol":"US30","Bid":"17134.00","Ask":"17136.00","Spread":"2.00","ProductType":"2",},{"Symbol":"USDSEK","Bid":"8.316","Ask":"8.31711","Spread":"11.10","ProductType":"1",},{"Symbol":"AUDCHF","Bid":"0.73659","Ask":"0.73664","Spread":"0.50","ProductType":"1",},{"Symbol":"GER30","Bid":"9921.00","Ask":"9922.00","Spread":"1.00","ProductType":"2",},{"Symbol":"USOil","Bid":"36.61","Ask":"36.66","Spread":"5.00","ProductType":"3",},{"Symbol":"GBPNZD","Bid":"2.13329","Ask":"2.13358","Spread":"2.90","ProductType":"1",},{"Symbol":"EURCAD","Bid":"1.48262","Ask":"1.48272","Spread":"1.00","ProductType":"1",},{"Symbol":"EURUSD","Bid":"1.11089","Ask":"1.1109","Spread":"0.10","ProductType":"1",},{"Symbol":"XPTUSD","Bid":"956.60","Ask":"959.70","Spread":"31.00","ProductType":"5",},{"Symbol":"EURJPY","Bid":"125.441","Ask":"125.449","Spread":"0.80","ProductType":"1",},{"Symbol":"AUS200","Bid":"5089.00","Ask":"5091.00","Spread":"2.00","ProductType":"2",},{"Symbol":"EURGBP","Bid":"0.78286","Ask":"0.7829","Spread":"0.40","ProductType":"1",},{"Symbol":"USDCAD","Bid":"1.33461","Ask":"1.33471","Spread":"1.00","ProductType":"1",},{"Symbol":"EURNOK","Bid":"9.48035","Ask":"9.48285","Spread":"25.00","ProductType":"1",},{"Symbol":"GBPCHF","Bid":"1.39949","Ask":"1.39961","Spread":"1.20","ProductType":"1",},{"Symbol":"GBPAUD","Bid":"1.89993","Ask":"1.90006","Spread":"1.30","ProductType":"1",},{"Symbol":"USDJPY","Bid":"112.919","Ask":"112.923","Spread":"0.40","ProductType":"1",},{"Symbol":"USDNOK","Bid":"8.5339","Ask":"8.5353","Spread":"14.00","ProductType":"1",},{"Symbol":"AUDCAD","Bid":"0.99675","Ask":"0.99684","Spread":"0.90","ProductType":"1",},{"Symbol":"ITA40","Bid":"18746.00","Ask":"18766.00","Spread":"20.00","ProductType":"2",},{"Symbol":"FRA40","Bid":"4458.50","Ask":"4459.50","Spread":"1.00","ProductType":"2",},{"Symbol":"AUDUSD","Bid":"0.74683","Ask":"0.74686","Spread":"0.30","ProductType":"1",},{"Symbol":"USDHKD","Bid":"7.75917","Ask":"7.75948","Spread":"3.10","ProductType":"1",},{"Symbol":"NZDCHF","Bid":"0.65595","Ask":"0.65607","Spread":"1.20","ProductType":"1",},{"Symbol":"EURTRY","Bid":"3.21593","Ask":"3.21737","Spread":"14.40","ProductType":"1",},{"Symbol":"AUDJPY","Bid":"84.333","Ask":"84.339","Spread":"0.60","ProductType":"1",},{"Symbol":"USDZAR","Bid":"15.90479","Ask":"15.9158","Spread":"110.10","ProductType":"1",},{"Symbol":"Bund","Bid":"161.49","Ask":"161.52","Spread":"3.00","ProductType":"4",},{"Symbol":"USDMXN","Bid":"17.88412","Ask":"17.88669","Spread":"25.70","ProductType":"1",},{"Symbol":"USDTRY","Bid":"2.8947","Ask":"2.89508","Spread":"3.80","ProductType":"1",},{"Symbol":"USDOLLAR","Bid":"12022.00","Ask":"12024.00","Spread":"2.00","ProductType":"7",},{"Symbol":"JPN225","Bid":"16926.50","Ask":"16936.50","Spread":"10.00","ProductType":"2",},{"Symbol":"HKG33","Bid":"20290.00","Ask":"20300.00","Spread":"10.00","ProductType":"2",},{"Symbol":"UK100","Bid":"6136.00","Ask":"6137.00","Spread":"1.00","ProductType":"2",},{"Symbol":"CADCHF","Bid":"0.73895","Ask":"0.73904","Spread":"0.90","ProductType":"1",},{"Symbol":"NAS100","Bid":"4353.00","Ask":"4354.00","Spread":"1.00","ProductType":"2",},{"Symbol":"ZARJPY","Bid":"7.097","Ask":"7.10","Spread":"0.30","ProductType":"1",},{"Symbol":"GBPCAD","Bid":"1.89381","Ask":"1.894","Spread":"1.90","ProductType":"1",},{"Symbol":"NGAS","Bid":"1.8745","Ask":"1.8845","Spread":"10.00","ProductType":"3",},{"Symbol":"ESP35","Bid":"8995.00","Ask":"9003.00","Spread":"8.00","ProductType":"2",},{"Symbol":"GBPUSD","Bid":"1.41899","Ask":"1.41906","Spread":"0.70","ProductType":"1",},{"Symbol":"SPX500","Bid":"2007.63","Ask":"2008.13","Spread":"5.00","ProductType":"2",},{"Symbol":"GBPJPY","Bid":"160.231","Ask":"160.244","Spread":"1.30","ProductType":"1",},{"Symbol":"XPDUSD","Bid":"561.70","Ask":"564.70","Spread":"30.00","ProductType":"5",},{"Symbol":"EUSTX50","Bid":"3060.00","Ask":"3061.00","Spread":"1.00","ProductType":"2",},{"Symbol":"TRYJPY","Bid":"38.986","Ask":"39.003","Spread":"1.70","ProductType":"1",},{"Symbol":"NZDCAD","Bid":"0.88767","Ask":"0.8878","Spread":"1.30","ProductType":"1",},{"Symbol":"EURNZD","Bid":"1.67013","Ask":"1.67029","Spread":"1.60","ProductType":"1",},{"Symbol":"XAUUSD","Bid":"1231.13","Ask":"1231.55","Spread":"42.00","ProductType":"5",},{"Symbol":"NZDUSD","Bid":"0.66508","Ask":"0.66516","Spread":"0.80","ProductType":"1",},{"Symbol":"SUI20","Bid":"7960.00","Ask":"7964.00","Spread":"4.00","ProductType":"2",},{"Symbol":"NZDJPY","Bid":"75.101","Ask":"75.108","Spread":"0.70","ProductType":"1",},{"Symbol":"UKOil","Bid":"38.98","Ask":"39.03","Spread":"5.00","ProductType":"3",},{"Symbol":"CHFJPY","Bid":"114.488","Ask":"114.494","Spread":"0.60","ProductType":"1",},{"Symbol":"EURCHF","Bid":"1.0957","Ask":"1.09574","Spread":"0.40","ProductType":"1",}]}); Everything returnes 'undefined'. Here is a fiddle
Your response is returning an array of objects. Iterating over that array should give you the data. Something like this could work: $(function(){ var API = "https://ratesjson.fxcm.com/DataDisplayer"; $.ajax({ url: API, dataType: 'jsonp', crossDomain: true, success:function( msg ) { var html = ''; var Symbol = msg.Rates.Symbol; for(var i = 0; i < msg.Rates.length; i ++) { html += '<tr><td>'+msg.Rates[i].Symbol+'</td><td>'+msg.Rates[i].Bid+'</td><td>'+msg.Rates[i].Ask+'</td><td>'+msg.Rates[i].Spread+'</td></tr>'; console.log("Symbol: " +msg.Rates[i].Symbol); } $('#dataTable tbody').html(html); } }); }); Updated fiddle: https://jsfiddle.net/igor_9000/5o9q8g0y/2/ Hope that helps!
Try with this : var Symbol = msg.Rates[0].Symbol; console.log(JSON.stringify(Symbol));
Try this- $(function(){ var API = "https://ratesjson.fxcm.com/DataDisplayer"; $.ajax({ url: API, dataType: 'jsonp', crossDomain: true, success:function( msg ) { $.each(msg.Rates, function ( key, value ) { console.log(key +":"+ value); }); } }); });
Can't append items to array with ajax [duplicate]
This question already has answers here: How do I return the response from an asynchronous call? (41 answers) Closed 8 years ago. I'm trying to append the link value of Instagram images into an array. The link values are showing up fine in the console but my array length is 0 after the ajax function. Also '0' shows up in the console before all the links. I have an idea this problem is caused by ajax running asynchronously and so the rest of the js code is actually run before anything can be appended to $images, but I have no idea how to circumvent this. This is my code: $liked = 'https://api.instagram.com/v1/users/self/media/liked?access_token=' + $access_token + '&COUNT=20'; $images = [] $.ajax({ type:'get', dataType:'jsonp', cache: false, url: $liked, success: function(data) { for (var i = 0; i < data.data.length; i++) { console.log(data.data[i].images.standard_resolution.url); $images.push(data.data[i].images.standard_resolution.url); } } }); console.log($images.length);
The console.log will run before the ajax call is ended; write your code or you console.log in your success function right after the end of the data loop. Like: $liked = 'https://api.instagram.com/v1/users/self/media/liked?access_token=' + $access_token + '&COUNT=20'; $images = [] $.ajax({ type:'get', dataType:'jsonp', cache: false, url: $liked, success: function(data) { for (var i = 0; i < data.data.length; i++) { console.log(data.data[i].images.standard_resolution.url); $images.push(data.data[i].images.standard_resolution.url); } console.log($images.length); // here the array is filled with response data } });
jquery async:false won't work [duplicate]
This question already has answers here: async-ajax call not working as expected (2 answers) Ajax synchronous requests (1 answer) Closed 8 years ago. So, I have a list of imdb ids stored in the database. What I want to do is, iterate over these and query a web service provider, for example TMDB and get the movie thumbnail associated with that imdb id. I have fetched the data from the database, and stored it in response. <script> var results_area = $(".results-area"); function render(response) { var img_thumbnail = $(".img-thumbnail"); var quote= $(".quote"); results_area.empty(); for (var i = 0; i < response.d.length; i++) { img_thumbnail.attr('src', getImageThumbnail(response.d[i].imdbId)); quote.text("Reviews:" + response.d[i].quote); results_area.append(img_thumbail.clone(),quote.clone()); } } function getImageThumbnail(imdbId) { $.ajax({ async: false, url: "https://api.themoviedb.org/3/movie/" + imdbId + "?", data: param, dataType: 'jsonp', success: function () { } }); } </script> Since my dataType is 'jsonp', async:false won't work. What is happening is since in my html img src="#", it stays the same after the for loop finishes. Is there a better way to do this?
"Unexpected token o" when using $.parseJSON [duplicate]
This question already has answers here: JSON+Node.js - Unexpected token o (2 answers) How can I access and process nested objects, arrays, or JSON? (31 answers) Closed 9 years ago. The following ajax call gives the following result: $.ajax({ type: "POST", url: //**My full URL goes here**, data: {sources: sources}, dataType: "json", success: function(data) { alert(data); alert(data.length); for (var i = 0; i < data.length; i++) { alert(data[i]); } } }); Result: data: [objject object],[objject object],[objject object] length: 3 in loop: [objject object] [objject object] [objject object] and the following code , in which I just added: var data = $.parseJSON(data); $.ajax({ type: "POST", url: //**My full URL goes here**, data: {sources: sources}, dataType: "json", success: function(data) { var data = $.parseJSON(data); alert(data); alert(data.length); for (var i = 0; i < data.length; i++) { alert(data[i]); } } }); The above code gives me the following error: Uncaught Syntax Error: Unexpected token o Why is that? Am I doing something wrong? How can I fix it?
The data is already a parsed object since you are passing dataType: 'json', so there is no need to parse it again. Again to debug and inspect the value of data, use console logging instead of alert(), like console.log(data)
Stop use alert to debug, use console.log instead. success: function(data) { // in the browser console, you will see the data structure. // then do what you want console.log(data); // ...