spiderwebdev
spiderwebdev

Reputation: 95

JSON-C is there a way to use json_object_object_add to add to the beginning of an object that is already populated?

I am working with JSON-C in C to add to an object.

Is there a way to use json_object_object_add to add to an existing object but at the beginning of the object?

Example:

json_object * jobj = json_object_new_object();
json_object_object_add(jobj,"Key 2", json_object_new_string("value 2"));

//put this at the top as first element
json_object_object_add(jobj,"Key 1", json_object_new_string("value 1")); 

Upvotes: 1

Views: 239

Answers (1)

DXRW4E
DXRW4E

Reputation: 1

here https://github.com/DXRW4E/JSON

#include <stdio.h>
#include "JSON.c"

int main() {

WCHAR wsJSONStr[] = L"{"  \
    L"  \"responseContext\": {"  \
    L"      \"Data\": \"85678875432\","  \
    L"      \"Params\": ["  \
    L"          {"  \
    L"              \"service\": \"GFEEDBACK\","  \
    L"              \"params\": ["  \
    L"                  [ 2222, 33333, 4444, 5555 ],"  \
    L"                  {"  \
    L"                      \"key\": \"abcd\","  \
    L"                      \"value\": \"0\","  \
    L"                      \"key2\": \"bbbbb\""  \
    L"                  },"  \
    L"                  {"  \
    L"                      \"key\": \"abcd2\","  \
    L"                      \"value\": \"false\""  \
    L"                  }"  \
    L"              ],"  \
    L"              \"params2\": {"  \
    L"                  \"key3\": \"abcd3\","  \
    L"                  \"value3\": \"False\""  \
    L"              }"  \
    L"          }"  \
    L"      ]"  \
    L"  }"  \
    L"}" ; //356
    
    LPWSTR lpszJSON = NULL, lpEnd = NULL, lpStrfy = NULL;
    HJSON hJSON = NULL, hJSKey = NULL, hJSInsert = NULL;
    DWORD dwLastError = 0, cchStr = 0;

    //lpszJSON = FileReadEx(lpszJSONPath);
    lpszJSON = &wsJSONStr[0]; cchStr = StrLen(lpszJSON); //wprintf(L"1. StrLen %d \n", cchStr);
    hJSON = JSONParse(lpszJSON, cchStr, &lpEnd);
    if (!hJSON) { wprintf(L"0. JSONParse ERROR %d \n, %ls", GetLastError(), lpEnd); return 0; }
    dwLastError = JSONOpenKeyEx(hJSON, L"responseContext.Params[0][1][1]", 31, &hJSKey); //// JSONOpenKey() does not support subkey and array index, example "items.description" or "items[1].description" return ERROR_FILE_NOT_FOUND
    if (dwLastError) { wprintf(L"0. JSONOpenKeyEx ERROR %d \n", dwLastError); JSONFree(hJSON); return 0; }
    else {
        dwLastError = JSONInsertKey(hJSKey, L"NewKeyName", 10, JSON_IS_STRING, &hJSInsert, 1);
        if (dwLastError) { wprintf(L"0. JSONInsertKey ERROR %d \n", dwLastError); JSONFree(hJSON); return 0; }
        else {
            dwLastError = JSONSetValue(hJSInsert, JSON_IS_STRING, L"1673458487", 10);
            if (dwLastError) { wprintf(L"3. JSONSetValue ERROR %d \n", dwLastError); }
            JSONClose(hJSInsert);
        }
        JSONClose(hJSKey);
    }
    lpStrfy = JSONStringify(hJSON);
    if (!lpStrfy) { wprintf(L"5. JSONStringify ERROR %d \n", GetLastError()); return 0; }
    wprintf(L"5. lpStrfy %ls\n", lpStrfy);
    //bReturn = FileWriteEx("/storage/emulated/0/Download/JSON_TEST/009_Parse.json", lpStrfy, StrLen(lpStrfy), (O_RDWR | O_CREAT | O_TRUNC), (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH), UTF_8);
    //if (!bReturn) { wprintf(L"6. FileWriteEx ERROR %d \n", GetLastError());; return 0; }
    MemFree(lpStrfy); lpStrfy = NULL;
    JSONFree(hJSON); hJSON = NULL; // or JSONClose(hJSON); //JSONClose() && JSONCloseHandle() && JSONCloseKey() - if Handle is Header run JSONFree();
    return 0;
}

se here for more examples https://github.com/DXRW4E/JSON/blob/main/JSON_Example.c

Upvotes: 0

Related Questions