json_loads() not returning json_t objects from json string.

898 views
Skip to first unread message

Keshav

unread,
Apr 23, 2013, 2:57:17 AM4/23/13
to jansso...@googlegroups.com
Hi,

In this code, I am passing json string to json_loads() function. When I try to extract number of elements in json, json_array_size(root) function is returning zero. Could you find out any possible error here?

        json_error_t error;

        json_t *root = json_loads(jsonStr, 0, &error);
        printf("Array size: %d\n", json_array_size(root));
        if(!root)
        {
            printf("error: on line %d: %s\n", error.line, error.text);
            return 1;
        }

Petri Lehtinen

unread,
Apr 23, 2013, 3:11:26 AM4/23/13
to jansso...@googlegroups.com
If jsonStr contains invalid JSON text, json_loads() returns NULL. When
passed NULL, json_array_size() returns 0.

Petri

Keshav

unread,
Apr 23, 2013, 4:52:24 AM4/23/13
to jansso...@googlegroups.com
jsonStr holds this string  {"localTime": {"localTime": "2013-4-3T2-30-15.5+1:30"}, "timeMode": 11, "timeZone": {"timeZone": "5"}}  and this validated using online http://jsonlint.com/

jsonStr was generated using janson library and now I want to parse this string to read key and value.

Petri Lehtinen

unread,
Apr 23, 2013, 5:00:12 AM4/23/13
to jansso...@googlegroups.com
Keshav wrote:
> jsonStr holds this string {"localTime": {"localTime":
> "2013-4-3T2-30-15.5+1:30"}, "timeMode": 11, "timeZone": {"timeZone": "5"}} and
> this validated using online http://jsonlint.com/ .
>
> jsonStr was generated using janson library and now I want to parse this string
> to read key and value.

Ah, but it's not an array! Use json_object_size() to get the size of a
JSON object.

Petri
Reply all
Reply to author
Forward
0 new messages