0

i have this JS code:

var actualDate;
var order;
var datatest;
var labels;
var lang;
var cached;

$.getJSON( "http://homel.vsb.cz/~mor03/TAMZ/cnb_json.php", function( data ) {
  var items = [];
  $.each( data, function( key, val ) {
    //items.push( "<li id='" + key + "'>" + val + "</li>" );
    //alert("<li id='" + key + "'>" + val + "</li>");
    if(key==="date") actualDate=val;
    if(key==="order") order=val;
    if(key==="data") datatest=val;
    if(key==="labels") labels=val;
    if(key==="lang") lang=val;
    if(key==="cached") cached=val;

  });

alert(actualDate);
alert(order);
alert(datatest);
alert(labels);
alert(lang);
alert(cached);
});

My JSON look like: {"date":"2017-03-27","order":"61","data":[{"country_label":"Austr\u00e1lie","curr_label":"dolar","unit":"1","code":"AUD","rate":"18.946"}, {"country_label":"Braz\u00edlie","curr_label":"real","unit":"1","code":"BRL","rate":"7.951"}, {"country_label":"Bulharsko","curr_label":"lev","unit":"1","code":"BGN","rate":"13.814"}, {"country_label":"\u010c\u00edna","curr_label":"renminbi","unit":"1","code":"CNY","rate":"3.609"}, {"country_label":"D\u00e1nsko","curr_label":"koruna","unit":"1","code":"DKK","rate":"3.632"}, {"country_label":"EMU","curr_label":"euro","unit":"1","code":"EUR","rate":"27.020"}, {"country_label":"Filip\u00edny","curr_label":"peso","unit":"100","code":"PHP","rate":"49.561"}, {"country_label":"Hongkong","curr_label":"dolar","unit":"1","code":"HKD","rate":"3.194"}, {"country_label":"Chorvatsko","curr_label":"kuna","unit":"1","code":"HRK","rate":"3.632"}, {"country_label":"Indie","curr_label":"rupie","unit":"100","code":"INR","rate":"38.153"}, {"country_label":"Indonesie","curr_label":"rupie","unit":"1000","code":"IDR","rate":"1.864"}, {"country_label":"Izrael","curr_label":"\u0161ekel","unit":"1","code":"ILS","rate":"6.866"}, {"country_label":"Japonsko","curr_label":"jen","unit":"100","code":"JPY","rate":"22.527"}, {"country_label":"Jihoafrick\u00e1 rep.","curr_label":"rand","unit":"1","code":"ZAR","rate":"1.964"}, {"country_label":"Ji\u017en\u00ed Korea","curr_label":"won","unit":"100","code":"KRW","rate":"2.237"}, {"country_label":"Kanada","curr_label":"dolar","unit":"1","code":"CAD","rate":"18.586"}, {"country_label":"Ma\u010farsko","curr_label":"forint","unit":"100","code":"HUF","rate":"8.710"}, {"country_label":"Malajsie","curr_label":"ringgit","unit":"1","code":"MYR","rate":"5.625"}, {"country_label":"Mexiko","curr_label":"peso","unit":"1","code":"MXN","rate":"1.318"}, {"country_label":"MMF","curr_label":"SDR","unit":"1","code":"XDR","rate":"33.823"}, {"country_label":"Norsko","curr_label":"koruna","unit":"1","code":"NOK","rate":"2.931"}, {"country_label":"Nov\u00fd Z\u00e9land","curr_label":"dolar","unit":"1","code":"NZD","rate":"17.528"}, {"country_label":"Polsko","curr_label":"zlot\u00fd","unit":"1","code":"PLN","rate":"6.337"}, {"country_label":"Rumunsko","curr_label":"nov\u00e9 leu","unit":"1","code":"RON","rate":"5.939"}, {"country_label":"Rusko","curr_label":"rubl","unit":"100","code":"RUB","rate":"43.671"}, {"country_label":"Singapur","curr_label":"dolar","unit":"1","code":"SGD","rate":"17.835"}, {"country_label":"\u0160v\u00e9dsko","curr_label":"koruna","unit":"1","code":"SEK","rate":"2.829"}, {"country_label":"\u0160v\u00fdcarsko","curr_label":"frank","unit":"1","code":"CHF","rate":"25.223"}, {"country_label":"Thajsko","curr_label":"baht","unit":"100","code":"THB","rate":"72.239"}, {"country_label":"Turecko","curr_label":"lira","unit":"1","code":"TRY","rate":"6.892"}, {"country_label":"USA","curr_label":"dolar","unit":"1","code":"USD","rate":"24.813"}, {"country_label":"Velk\u00e1 Brit\u00e1nie","curr_label":"libra","unit":"1","code":"GBP","rate":"31.273"}],"labels":["zem\u011b","m\u011bna","mno\u017estv\u00ed","k\u00f3d","kurz"],"lang":"cs","cached":true}

I dont understand why datatest variable contains only [Object object],[Object object],... etc.

How to parse it into some array of objects? I want each country in some object and i need access to their values like AllCountrys[].country_label or something like this.

1
  • can you make a fiddle Commented Mar 27, 2017 at 19:21

1 Answer 1

1

Because your "data" element in your returned JSON is an array of Objects. Thus, when you assign datatest=val; you are assigning an array of Objects to the datatest variable.

If you try alert(datatest[0]["country_label"]) I expect you to see "Austr\u00e1lie" (properly encoded, of course).

Sign up to request clarification or add additional context in comments.

4 Comments

Sure, great.. datatest[1].country_label is working. Ty
My pleasure - glad to be of help. Mark the answer as correct if you so believe (<-- shameless self promotion).
Sure, but you must wait :D your answer help too soon. It say i must wait 4 minute to mark the answer
Clearly I need to ask more questions - thank you for your honesty and hope you find great success with this project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.