Loading a JSON file in a cocos creator project?

Note that I’m very new to this. I have a simple JSON file under assets and I want to load it in a variable like I do in other langages.

How can I do that?

Something like that:

var HeroDefault = ccs.load(“HeroDefault.json”);

I get the error that
Simulator : HeroDefault.json does not exist

When I use the simulator.

What is the best way to load a JSON file in the creator? What Am I doing wrong?

Thanks!

Hi, try this…

  1. right place:
    put u’r json file to path “assets/resources/HeroDefault.json”
    if assets not has resources sub folder, create it.
  2. Coding:
var url = cc.url.raw( 'resources/HeroDefault.json' )
cc.loader.load( url, function( err, res)
{
    cc.log( 'load['+ url +'], err['+err+'] result: ' + JSON.stringify( res ) );
});

then you will see the json file dump to log.

4 Likes

It worked, thanks alot.

how we can use the json in cocos creator 2.1.0

var t_Setsjson;

var url = cc.url.raw( ‘resources/IV_SetsJSON.json’ )

cc.loader.load( url, function( err, t_Setsjson)

{

cc.log( ‘load[’+ url +’], err[’+err+’] result: ’ + JSON.stringify( t_Setsjson ) );

});
starting its working after buid in browser getting error can u check please

after running in browser i am getting error called

Failed to load resource: the server responded with a status of 404 (Not Found) [http://localhost:7456/resources/IV_SetsJSON.json]

This is a 3 year old topic, why not start a new topic?

hi,
I just need the one line of code for reading a string containing a json and making a json object.
for e.g…
var txt = {“name”:“sam”};
var objj0 = JSON.stringify(txt);
var txt2 = objj0[“name”];
console.log(txt2);
it prints undefined in the console.
what is wrong and how can I assign the value of txt2 to another variable of type string?
thanks

because you are reading it as an object after you converted it to string

Hi,
I guess what you are trying to do is following:

var txt = '{"name":"sam"}';   //String in JSON format, please notice the single quotes surrounding the string
var obj0 = JSON.parse(txt); //Take text and create object
var txt2 = obj0["name"];
console.log(txt2); //this *should* print out "sam"

JSON.stringify(obj) takes an object and converts it to a string, but considering your purpose description, I guess you want to take a string and convert it to an object, which can be achieved by JSON.parse(string)

Create new a js, and call new variable equal that json, then export module and require it in other js
Example:
Create new js file named “Data.js”
var new_json = […] //content your json you need loaded
var new_json2 = […] //content your json you need loaded

module.exports = { //export data
new_json: new_json,
new_json1: new_json1,

}

Then, in other js, you need require this file

var {new_json, new_json1, …} = require(’./Data’)

Hope resolve for your problem :slight_smile:

NewProject_1 (1).zip (287.3 KB)
i have typescript code with loading json, can you give me javascript code for loading json i got errors …

Hi, you can try this:
put your json file to path “assets/resources/config.json”
cc.loader.loadRes(‘config’, cc.JsonAsset, function( err, res) {
cc.log("data: ", res.json);
});

I tried with Cocos Creator v2.1.3 and it working
Hope resolve for your problem :slightly_smiling_face: :slightly_smiling_face:

No sir, iam getting config as error


i have json data , i want load in content scrollview.i want json loading in content code,not for json loading in cc,log cosole.i want to see json data in scrollview in content. Any one please help me

this is my json text data, in this 13 lines of text, i want to load in scrollview in content,any one give code in javascript.

,

with properties cc.class also give ??

I think you should change your json file like this

{
    "SymptomsData": [
        "1....",
        "2....",
        "3...."
    ]
}

And this is my code load data from json file in scrollview in content

cc.loader.loadRes('jsondummy', cc.JsonAsset, function( err, res) {

    var dataJson = res.json.SymptomsData;

    for(let element in dataJson) {

        this.scrollView.content.addChild(element);

    }

}.bind(this));

Sir, this is small task but i cann’t do it??? where is the mistake done i don’t know, i tried your code also but don’t get it,i am using cocos creator v 2.2.1 version.

  1. can you please make me a design and code ,take any json text data ,that will be displayed on content in scrolleview.
  2. waiting for you responce

you change line 43 code like this:

this.content.addChild(element);