0

I'm trying to parse a JSON file received by a CGI file on a webserver using cJSON however the first number in the JSON keeps getting changed to 0.

I've made a short piece of code which I'm using to test this:

int main(int argc, char *argv[])
{
    cJSON *pstReq;
    char *pcReq = getenv("QUERY_STRING");

    printf("Content-Type: application/json\n\n");

    URLDecode(pcReq) /* Decodes the query string to JSON string */
    pstReq = cJSON_Parse(pstReq);

    printf("%d\n", cJSON_GetObjectItem(pstReq, "test")->valueint);
    printf("%d\n", cJSON_GetObjectItem(pstReq, "test2")->valueint);
    printf(cJSON_Print(pstReq));

    return EXIT_SUCCESS;
}

Passing the JSON {"test":123, "test2":123} into this through the query string causes the program to output this:

0
123
{"test":0, "test2":123}

I have absolutely no idea what I'm doing wrong here if anybody could give me some idea of what the problem might be I'd greatly appreciate it.

Sled
  • 18,541
  • 27
  • 119
  • 168
KGB
  • 3
  • 1

1 Answers1

0

It is difficult to know without knowing how URLDecode works, or what are the original contents of pcReq after it is retrieved from the environment. I would start from running this code without the web, to test cJSON as a small unit, and that would probably shed light on what is going wrong.

First, to help us understand your code, in the code below:

pstReq = cJSON_Parse(pstReq);

I think that you meant:

pstReq = cJSON_Parse(pcReq);

With that in mind, I would run first this code:

#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"

int main(int argc, char *argv[])
{
    cJSON *pstReq;

    char *pcReq = "{\"test\":123, \"test2\":123}";
    pstReq = cJSON_Parse(pcReq);

    printf("%d\n", cJSON_GetObjectItem(pstReq, "test")->valueint);
    printf("%d\n", cJSON_GetObjectItem(pstReq, "test2")->valueint);
    printf("%s\n",cJSON_Print(pstReq));

    return EXIT_SUCCESS;
}

This works as expected for me.

If this produces the right output for you too, I would add printf()'s to see what is contained in pcReq before and after URLDecode(). The problem might come from 'pcReq' itself. So in short this would be the code that might give you an idea of where the problem is:

int main(int argc, char *argv[])
{
    cJSON *pstReq;
    char *pcReq = getenv("QUERY_STRING");

    printf("Content-Type: application/json\n\n");

    printf ("=== pcReq before decoding ===\n");
    printf("%s\n",pcReq);
    printf ("=============================\n");

    URLDecode(pcReq); /* Decodes the query string to JSON string */

    printf ("=== pcReq after decoding ===\n");
    printf("%s\n",pcReq);
    printf ("=============================\n");

    pstReq = cJSON_Parse(pcReq);

    printf("%d\n", cJSON_GetObjectItem(pstReq, "test")->valueint);
    printf("%d\n", cJSON_GetObjectItem(pstReq, "test2")->valueint);
    printf("%s\n",cJSON_Print(pstReq));

    return EXIT_SUCCESS;
}

I hope this helps to find the problem.

Jorge Torres
  • 1,426
  • 12
  • 21
  • Thanks for the reply, I tried running the first piece of code you posted on my linux machine and sure enough everything ran as expected. However when I added the line `printf("Content-Type: application/json\n\n");` and attempted to run it on the system I had originally been running the program on (a Cubox) I get the same incorrect output I had before. This leads me to believe that the code is actually correct and the problem is being caused by some other factor related to the Cubox and how it's set up. – KGB Feb 12 '15 at 18:44