Jump to content

need help understand a json file to read it with ajax


Colon
 Share

Recommended Posts

I am trying to read a json file in my js with ajax. And I am not sure about that json file, what is an object, what is an array.

.json file

"{
"people": [
{
  "answers": [
    {
      "subject": "home",
      "suggest": 2
    },
    {
      "subject": "livelihood",
      "suggest": 2
    },
    {
      "subject": "childhood",
      "suggest": 1
    }
  ],
  "class": {
    "detail": "commoner",
    "exclusions": [
      {
        "category": "childEvents",
        "detail": "apprentice"
      }
    ],
    "selectWeight": 0.889
  },
  "correlations": [],
  "default-ask": [
    {
      "text": "a text",
      "topics": []
    }
  ],
    .....
} ] }

people is an array, in people we have one object which contains all the following stuff (answers, correlations, default-ask,...)?
answers is an array in people
in answers are 3 objects.
the next is class is that an object which contains an id detail, a value selectWeight and an array exclusions?
then an other array correlations
and an array default-ask with an object with the id text and an array topics

I know how to read out answers but not how to read out the rest. can you help me with that please.

.js file

$.ajax({
        url: 'assets/articles.json',
        dataType: 'json',
        type: 'get',
        cache: false,
        success: function(data){
            $(data.people[0].answers).each(function(index, value){
                console.log(value.subject + ' ' +  value.suggest)
            })    
        }     
    });

 

Link to comment
Share on other sites

try this

$.ajax({
            url: 'assets/articles.json',
            dataType: 'json',
            type: 'get',
            cache: false,
            success: function(data){
                console.log(data.people[0].class.detail);
                console.log(data.people[0].class.selectWeight);
                
                $(data.people[0].class.exclusions).each(function(index, value){
                    console.log(value.category + ' ' +  value.detail)
                
                
                })
            }
            
        });

 

oh cool, thanks ?

Link to comment
Share on other sites

If you don't use jquery you may try this:

function eAjaxRequest(url, completeFunction, params) {
    var request = this;
    
    this.requestUrl = url;
    this.callback = completeFunction || function () { };
    this.postParams = params;
    this.onprogress = function() { };
    this.post = function(params){
        request.http = null;
        var activexmodes = ["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
        if (window.XMLHttpRequest){ // if Mozilla, Safari etc
            request.http = new XMLHttpRequest();
        }else if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
            for (var i=0; i < activexmodes.length; i++){
                try{
                    request.http = new ActiveXObject(activexmodes[i]);
                }
                catch(e){
                    console.log(e);
                    //suppress error
                }
            }
        }
        if (request.http == null) {
            return false;
        }else{
            request.http.onprogress = function(evt){
                request.onprogress(String(evt.loaded / evt.total));
            }
            request.http.onreadystatechange = function(){
                if(request.http.readyState == 4){
                    if(this.status == 200 || window.location.href.indexOf("http") == -1){
                        request.callback(request.http.responseText, request.http.status, request.http.responseXML);
                        request.http = null;
                    }else{
                        console.log("Error loading Ajax data");
                    }
                }
            }
            request.http.open("POST", this.requestUrl, true);
            request.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            if(params){
                request.http.setRequestHeader("Content-Length", params.length);
                request.http.send(params);
            }else{
                request.http.send();
            }
            return true;
        }
    }

    this.post(params);
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...