Ambiera Forum

Discussions, Help and Support.

Ambiera Forum > CopperCube > Help with CopperCube
working with JSON in coppercube

Guest
Guest
Quote
2022-11-01 14:49:54

hello everyone, I'm writing a multiplayer game and my server is using JSON. question: how to parse JSON in coppercube?
In browser or nodejs i can do that:
var json = '{"a": "b"}'; 
JSON.parse(json);

but this code not working in coppercube (error: JSON is undefined), coppercube doesn't API to working with JSON?


okeoke
Registered User
Quote
2022-11-01 16:47:05

Hi,

Coppercube uses pre es-5 version of js, which doesn't have JSON object api yet.

You can use a polyfill. The simplest one would be:
JSON = { 
parse: function (sJSON) { return eval('(' + sJSON + ')'); },
stringify: function (vContent) {
if (vContent instanceof Object) {
var sOutput = '';
if (vContent.constructor === Array) {
for (var nId = 0; nId < vContent.length; sOutput += this.stringify(vContent[nId]) + ',', nId++);
return '[' + sOutput.substr(0, sOutput.length - 1) + ']';
}
if (vContent.toString !== Object.prototype.toString) {
return '"' + vContent.toString().replace(/"/g, '\\$&') + '"';
}
for (var sProp in vContent) {
sOutput += '"' + sProp.replace(/"/g, '\\$&') + '":' + this.stringify(vContent[sProp]) + ',';
}
return '{' + sOutput.substr(0, sOutput.length - 1) + '}';
}
return typeof vContent === 'string' ? '"' + vContent.replace(/"/g, '\\$&') + '"' : String(vContent);
}
};


You can also google json2 and json3, I believe, these implementations could also be adapted to use in Coppercube, but the one above usually works for me.


Guest
Guest
Quote
2022-11-02 12:52:20

Oh, it's works, thank you!


Create reply:


Posted by: (you are not logged in)


Enter the missing letter in: "Internatio?al" (you are not logged in)


Text:

 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


   






Copyright© Ambiera e.U. all rights reserved.
Privacy Policy | Terms and Conditions | Imprint | Contact