Using Json.cpp in Cocos2dx v4

105 views Asked by At

https://github.com/cocos2d/cocos2d-x/blob/v4/cocos/editor-support/spine/Json.cpp

I need help loading a .txt and pulling out some text using cocos for an old App. Can anyone work up a simple example?

The backstory is that I wrote a working app about 5-6 years ago when cocos used a different json library. They changed the library and I can't decipher the new one enough to get it working again. I am not a programmer, but made the app as a favor for a hospital. The json is used to switch between languages for the script. I don't really even know how to ask a technical question about the library. I know the code is all there, but I don't know how to make it work...

Thanks :)

3

There are 3 answers

1
Stephen On

cocos2dx v4 Json implementation

This is what I figured out eventually. Any improvements you can suggest are welcome.

0
Peter Andela On

I use this to read json-response from a translation api:

std::vector<char> * buffer = response->getResponseData();
char * concatenated = (char *) malloc(buffer->size() + 1);
std::string s2(buffer->begin(), buffer->end());
strcpy(concatenated, s2.c_str());
CCLOG ("DEBUG |%s|", concatenated);

Json * json = Json_create(concatenated);
Json *responseData = Json_getItem(json, "responseData");

const char * var22 = Json_getString(responseData, "translatedText", "default");

USED JSON response {"responseData": {"translatedText":"ni\u00f1o"}, .....

and copyed the old json.c and json.h in my classes dir.

0
Peter Andela On
static void readCurve (Json* frame, spCurveTimeline* timeline, int frameIndex) {
Json* curve = Json_getItem(frame, "curve");
if (!curve) return;
if (curve->type == Json_String && strcmp(curve->valueString, "stepped") == 0)
    spCurveTimeline_setStepped(timeline, frameIndex);
else if (curve->type == Json_Array) {
    Json* child0 = curve->child;
    Json* child1 = child0->next;
    Json* child2 = child1->next;
    Json* child3 = child2->next;
    spCurveTimeline_setCurve(timeline, frameIndex, child0->valueFloat, child1->valueFloat, child2->valueFloat,
            child3->valueFloat);
}

}