Skip to main content
EnZhoCR
Participating Frequently
August 16, 2023
Question

Reading Json from external file

  • August 16, 2023
  • 2 replies
  • 164 views

This is my Json :

{"MatchOnCourt":{"MatchID":"42959157","Tournament":"Ystad, Singles Qualifying, M-ITF-SWE-03A","P1Name":"ROBIN","P1Surname":"THOUR","P2Name":"OLLE","P2Surname":"NOLTORP","NOC1":"\u0026#160;","NOC2":"\u0026#160;","Score1":"0","Score2":"0","P1s1":"3","P1s2":"0","P1s3":"\u0026#160;","P2s1":"6","P2s2":"1","P2s3":"\u0026#160;","Serve":"1","Time":"00:49:59","Status":"SECOND_SET","CourtID":1,"Sponsor":null,"TournamentGroupID":777,"CourtName":"Court 4","Custom":null,"Info":"Break [T2] (double fault)","SRadarTournamentID":135793,"SRadarCategory":"ITF Men","Order":1}}

 

Code:

	var that = this;
	this.json = {};

	this.loadJson = function(url)
	{

		var queue = new createjs.LoadQueue(true);
		queue.on("fileload", that.onJsonLoaded, that);
		queue.on("error", that.onJsonLoadError, that);
		queue.loadFile({id:"json", src:url});

	}

	this.onJsonLoaded = function(e)
	{	
		that.json = e.target.getResult("json");

		console.log(that.json)
		
		var jMat = that.json.MatchOnCourt;
		
		console.log("0 +++++ " + jMat.Time)	

	}

	this.onJsonLoadError = function(e)
	{
	  console.log(e);
	}

	this.loadJson(URL);

 

What i am trying to do is to read the value time, i can load the Json (beacuse the firs console.log write my all the json file...) but i can not get "Time" value. What am i doing wrong?

 

Thanks 

    This topic has been closed for replies.

    2 replies

    kglad
    Community Expert
    Community Expert
    August 16, 2023

    for others, this may make more sense:

     

    loadJasonF.bind(this)("json_id.json");
     
    function loadJasonF (url){
    var queue = new createjs.LoadQueue(true);
    queue.addEventListener("fileload", jasonLoadedF.bind(this));
    queue.addEventListener("error", jasonLoadErrorF.bind(this));
    queue.loadFile({
    id: "json",
    src: url
    });
    }
     
    function jasonLoadedF(e){
    this.json = e.target.getResult("json");
    this.data = this.json.data;
    textF.bind(this)();
    }
     
    function jasonLoadErrorF(e){
    console.log(e);
    }
    function textF(){
    for (var i = 0; i < this.data.length; i++){
    this.txt.text = this.txt.text + "\n" + this.data[i].id + ", " + this.data[i].name + ", " + this.data[i].class +", " + this.data[i].mark + ", " + this.data[i].sex;
    }
    }
     
    /////////////////////////////////////////////
    where json_id.json contains
     

    {
    "data":
    [
    {"id":"1","name":"John Deo","class":"Four5",
    "mark":"75","sex":"male"},

    {"id":"2","name":"Max Ruin","class":"Three5",
    "mark":"85","sex":"male"},

    {"id":"3","name":"Arnold","class":"Three5",
    "mark":"55","sex":"male"},

    {"id":"4","name":"Krish Star","class":"Four5",
    "mark":"60","sex":"male"}
    ]
    }

    EnZhoCR
    EnZhoCRAuthor
    Participating Frequently
    August 16, 2023

    I find the problem.... i missed parsing to Json ... ( var jMat = JSON.parse(that.json); )

     

    stupid error...