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
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,
…
}
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
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
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.
can you please make me a design and code ,take any json text data ,that will be displayed on content in scrolleview.