This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

cJSON error

Dear Sir.

I am using the cJSON driver .

When it comes to string it works fine .

However when I use integer and swap the lines

//cJSON_AddStringToObject(root, "payload", payload);
cJSON_AddNumberToObject(root, "payload", 1243);

I can use the debug , The program is halted.

Please Advise.

cJSON * createJsonFromParams(void)
{
  
  char * command = "periodic" ;//  "holter"  "holtersm" "spot"   "time"
  char * type  = "12";
//  char * start_time  = "1845";
//  char * duration  = "104";
//  char * rest  = "29";
//  char * stop_time  = "27000";
  char * payload =  "Etrog2020";
//  int * payload = 123456; //"Etrog2020"; //  {0x06,0x05,0x04,0x03,0x02,0x01}; //
//  cJSON *number_item = cJSON_CreateNumber(1942);

  cJSON *root = cJSON_CreateObject();
  cJSON_AddStringToObject(root, "command", command);
  cJSON_AddStringToObject(root, "type", type);
//  cJSON_AddStringToObject(root, "start_time", start_time);
//  cJSON_AddStringToObject(root, "duration", duration);
//  cJSON_AddStringToObject(root, "rest", rest);
//  cJSON_AddStringToObject(root, "stop_time", stop_time);



  cJSON_AddStringToObject(root, "payload", payload);
//  cJSON_AddNumberToObject(root, "payload", 1243);

  
  return root;
}

Parents
  • Hi

    This problem is over .

    I added the following lines to prj.conf .

    CONFIG_CJSON_LIB=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

    However a new problem that I encounter,

    I do able to produce a JSON string .

    In the string I insert a array , I can't fill the array with data like [12,13,45,234,34,5,99]

    The array is always empty [] ,

    I use the following code.

    .

    cJSON * createJsonFromParams(void)
    {
    
      char * command = "periodic" ;//  "holter"  "holtersm" "spot"   "time"
      char * type  = "8";
    //  char * start_time  = "1845";
    //  char * duration  = "104";
    //  char * rest  = "29";
    //  char * stop_time  = "27000";
    //  char * payload =  "Etrog2020";
    //  int * payload = 123456; //"Etrog2020"; //  {0x06,0x05,0x04,0x03,0x02,0x01}; //
    //  cJSON *number_item = cJSON_CreateNumber(1942);
    
      cJSON *root = cJSON_CreateObject();
      cJSON_AddStringToObject(root, "command", command);
      cJSON_AddStringToObject(root, "type", type);
    
     
       cJSON * payload = cJSON_CreateArray();
    
     
       cJSON_AddItemToObject(root,payload, cJSON_CreateNumber(129));
        cJSON_AddItemToArray(payload, cJSON_CreateNumber(33));
    
    
        // Write the array "payload" to stream.
        cJSON_AddArrayToObject(root, "payload");
    
     
    
      return root;
    }

    This is the JSON Iget

    Thanks

Reply
  • Hi

    This problem is over .

    I added the following lines to prj.conf .

    CONFIG_CJSON_LIB=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y

    However a new problem that I encounter,

    I do able to produce a JSON string .

    In the string I insert a array , I can't fill the array with data like [12,13,45,234,34,5,99]

    The array is always empty [] ,

    I use the following code.

    .

    cJSON * createJsonFromParams(void)
    {
    
      char * command = "periodic" ;//  "holter"  "holtersm" "spot"   "time"
      char * type  = "8";
    //  char * start_time  = "1845";
    //  char * duration  = "104";
    //  char * rest  = "29";
    //  char * stop_time  = "27000";
    //  char * payload =  "Etrog2020";
    //  int * payload = 123456; //"Etrog2020"; //  {0x06,0x05,0x04,0x03,0x02,0x01}; //
    //  cJSON *number_item = cJSON_CreateNumber(1942);
    
      cJSON *root = cJSON_CreateObject();
      cJSON_AddStringToObject(root, "command", command);
      cJSON_AddStringToObject(root, "type", type);
    
     
       cJSON * payload = cJSON_CreateArray();
    
     
       cJSON_AddItemToObject(root,payload, cJSON_CreateNumber(129));
        cJSON_AddItemToArray(payload, cJSON_CreateNumber(33));
    
    
        // Write the array "payload" to stream.
        cJSON_AddArrayToObject(root, "payload");
    
     
    
      return root;
    }

    This is the JSON Iget

    Thanks

Children
  • Hi, Ephraim!

    Good that you are progressing. Some comments:
    - One line 22 of your code you are adding a number object to the array in the wrong way, which in itself may cause trouble.
    - On line 27 you are creating an entirely new array. If you want to add the existing array use cJSON_AddItemToObject(root, "payload", payload).

    Anyway, I got this working with the code below: 

    /*
     * Copyright (c) 2012-2014 Wind River Systems, Inc.
     *
     * SPDX-License-Identifier: Apache-2.0
     */
    
    #include <zephyr.h>
    #include <sys/printk.h>
    #include <cJSON.h>
    #include <cJSON_os.h>
    
    void createJsonFromParams(void)
    {
    	char *command = "periodic"; //  "holter"  "holtersm" "spot"   "time"
    	char *type = "8";
    
    	cJSON *root = cJSON_CreateObject();
    	cJSON_AddStringToObject(root, "command", command);
    	cJSON_AddStringToObject(root, "type", type);
       //cJSON_AddNumberToObject(root, "number", 33);
    
       cJSON * payload = cJSON_AddArrayToObject(root, "payload");
    
       cJSON_AddItemToArray(payload, cJSON_CreateNumber(33));
    
       printk("%s",cJSON_Print(root));
       cJSON_Delete(root);
    }
    
    void main(void)
    {
       cJSON_Init();
    
    	while (1) {
          createJsonFromParams();
    		k_sleep(K_MSEC(5000));
    	}
    }
     

    And the following prj.conf:

    CONFIG_SERIAL=y
    CONFIG_DK_LIBRARY=y
    CONFIG_CJSON_LIB=y
    CONFIG_NEWLIB_LIBC=y
    CONFIG_NEWLIB_LIBC_FLOAT_PRINTF=y
    CONFIG_ASSERT=y
    
    CONFIG_HEAP_MEM_POOL_SIZE=8192
    CONFIG_MAIN_STACK_SIZE=8192


    Note that cJSON_Init is called at the start of main. This enables use and printing of the number object.

    Best regads,
    Carl Richard

  • Hello ,

    I know this topic is old, but I have this problem and apparently this solution is no longer working, could you give another solution to work with numbers through the cJSON lib?

    I have no problem using cJSON_AddItemToObject, I can send strings, but when I try to send any number I find the problem. I have also configured:

    CONFIG_MAIN_STACK_SIZE=16384

    CONFIG_HEAP_MEM_POOL_SIZE=16384

    I inserted all the recommendations both in prj.conf, and in relation to libraries and cJSON_Init at the beginning of main, but I keep getting a Hard Fault, could you check if this solution still works for you? I'm using NCS 1.8.0!

Related