Code Monkey home page Code Monkey logo

Comments (4)

mbratch avatar mbratch commented on June 9, 2024

According to the cJSON documentation on github:

Use cJSON_GetArrayItem to get an element at a given index.

from cjson.

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

I don't understand how to do it right.
SetAP key value no.

if (root != NULL) {
  cJSON * key = NULL;
  cJSON_ArrayForEach(key, root) {
    if (cJSON_IsString(key) && (key->valuestring != NULL)) {

      if (!strcmp(key->string, "SetSTA")) {
        cJSON * value_data = cJSON_GetObjectItem(root, "SetSTA");
        if (!cJSON_IsArray(value_data)) {
          cJSON_Delete(root);
          return;
        }
        const cJSON * arrayItem = cJSON_GetArrayItem(value_data, 0);
        send_uart_0("SYS", "WS <= Value:", arrayItem->valuestring);
      } else send_uart_0("ER!", "WS Key", "Unknown");
    }
  }
}

from cjson.

daschfg avatar daschfg commented on June 9, 2024

Your call to cJSON_ArrayForEach doesn't make sense in this context, as your root is a json object. You can just omit this call.
And as mbratch said: If you want to adress the entries by index, just use cJSON_GetArrayItem on the extracted array. Somewhat like this:

    if (root != NULL)
    {
        cJSON* value_data = cJSON_GetObjectItem(root, "SetSTA");
        if (!cJSON_IsArray(value_data))
        {
            cJSON_Delete(root);
            return;
        }

        const cJSON* ssid = cJSON_GetArrayItem(value_data, 0);
        const cJSON* pass = cJSON_GetArrayItem(value_data, 1);

        printf("Name: %s\r\n", cJSON_GetStringValue(ssid));
        printf("Pass: %s\r\n", cJSON_GetStringValue(pass));
    }

from cjson.

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

I figured it out, but I'm not sure how true this is

// { "SetSTA": ["SSID_Name", "Password"] } - rxData

cJSON * root = cJSON_Parse(rxData);
if (root != NULL) {
  send_uart_0("SYS", "WS <= SRC", rxData);

  /* Перебираем ключи: key->string - ключ, key->valuestring - значение */
  cJSON * key = NULL;
  cJSON_ArrayForEach(key, root) {

    /* Настройки в режиме STA */
    if (!strcmp(key->string, "SetSTA")) {
      cJSON * SetSTA_Data = cJSON_GetObjectItem(root, "SetSTA");
      if (SetSTA_Data == NULL) {
        cJSON_Delete(root);
        free(rxData);
        return;
      }
      strncpy(config.StaSSID, cJSON_GetStringValue(cJSON_GetArrayItem(SetSTA_Data, 0)), sizeof(config.StaSSID));
      strncpy(config.StaPSK, cJSON_GetStringValue(cJSON_GetArrayItem(SetSTA_Data, 1)), sizeof(config.StaPSK));
      flag_file_system = FS_SAVE_CONFIG_FILE;
    } else send_uart_0("ER!", "WS Key", "Unknown");
  }

  if (str != NULL) free(str);
} else send_uart_0("ER!", "WS <=", "Package No JSON");

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.