HAF
HAF

Reputation: 3

When using the cJSON library to parse JSON string arrays, the correct output content was not obtained

I did not get the correct output when parsing JSON string arrays using the cJSON library Specifically, the string is not displayed in the output window. Code:

void FileReading(char *p) {
    char* TableFilePath = p;// is "Table.json" file path
    char* GameName;

    // Open JSON File
    FILE *fp = fopen(TableFilePath, "r");
    if (fp == NULL) {//如果文件未找到
        HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
        SetConsoleTextAttribute(hConsole, 0xc);//红色
    }

    //将文件内容读入字符串
    char buffer[1024];
    int len = fread(buffer, 1, sizeof(buffer), fp);
    fclose(fp);

    //解析JSON数据
    cJSON *json = cJSON_Parse(buffer);
    if (json == NULL) {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL) {
            HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
            SetConsoleTextAttribute(hConsole, 0xc);//红色
            printf("ERROR: %s\n", error_ptr);
            system("pause");
        }
        cJSON_Delete(json);
        return ;
    }

    //访问JSON数据
    char *i;
    char *str_tmp;

    cJSON *Localization, *str_tmp;

    cJSON * array = cJSON_GetObjectItem(json, "Localization");
    str_tmp = cJSON_GetObjectItem(Localization, "EN")->valuestring;
    if(str_tmp == NULL)
    {
        //for (i =  ; i < cJSON_GetArraySize(str_tmp) ; i++) {
            //printf("%s \n ",cJSON_GetArrayItem(str_tmp, i)->valueint);
            printf("id: %s\n", str_tmp);
        //}
    } else {printf("xxxxxxxxxxxxxxxx");}
}

JSON Code:

{
  "GameName": "Plants vs. Zombies",
  "GameProcessName":"Plants vs. Zombies",
  "ModifiableItem":"2",
  "BaseAddressTable":[
    {
      "0x24056588":"99999",
      "ModificationType":""
    },
    {
      "BaseAddressValue":"",
      "ModificationType":""
    }
  ],
  "id":["PZ01","PZ02"],
  "BaseAddressName":["0x24056588",""],
  "Localization":{
    "CN":["无限阳光","无限金币"],
    "EN":["Infinite sunshine","Infinite"]
  }
}

The fault code is under the comment 'Accessing JSON Data'. The fault code is under the comment '//访问JSON数据'.

The JSON string array to be obtained is located under 'Localization' in 'EN'

How can the code be modified to output the contents of the "EN" string array?

The above code has already been the last one I modified.

Upvotes: 0

Views: 133

Answers (0)

Related Questions