Code Monkey home page Code Monkey logo

Comments (3)

mbratch avatar mbratch commented on June 9, 2024

The functions to do this already exist. Did you try using cJSON_CreateString and cJSON_AddItemToArray?

from cjson.

daschfg avatar daschfg commented on June 9, 2024

To expand on this: For a general idea how to do it, please have a look at the two Printing examples in the Readme.
In particular how the array resolutions is constructed and added to.
You would have to use cJSON_CreateString instead of cJSON_CreateObject.

from cjson.

Oleg-Perevyshin avatar Oleg-Perevyshin commented on June 9, 2024

Yes, I looked at the example, but the documentation is not enough.
Thanks for the help!
My code:

char * langs_list(void) {
  File root = LittleFS.open("/langs", "r");
  if (!root || !root.isDirectory()) {
    return "[]";
  }
  File file = root.openNextFile();
  cJSON * langs_array = cJSON_CreateArray();
  if (langs_array == NULL) {
    cJSON_Delete(langs_array);
    return "[]";
  }
  while (file) {
    const char * fileName = file.name();
    char lang_name[3] = "";
    lang_name[0] = fileName[0];
    lang_name[1] = fileName[1];
    lang_name[2] = '\0';
    cJSON * name = cJSON_CreateString(lang_name);
    cJSON_AddItemToArray(langs_array, name);
    file = root.openNextFile();
  }
  char * langs_list = cJSON_PrintUnformatted(langs_array);
  if (langs_list == NULL) langs_list = "[]";
  cJSON_Delete(langs_array);
  return langs_list;
}

// In code
char * str = NULL;
str = langs_list();
//...
if (str != NULL) free(str);

from cjson.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.