Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recursively parse an array of json objs #51

Closed
ghost opened this issue Oct 20, 2021 · 5 comments
Closed

Recursively parse an array of json objs #51

ghost opened this issue Oct 20, 2021 · 5 comments

Comments

@ghost
Copy link

ghost commented Oct 20, 2021

Hi!

I was just testing the library for the first time and couldn't find a way to parse something like the following:

{"people": [ {"name":"Alice", "age": 40}, {"name": "John", "age": 38} ]}

Is there a (optimal) way to do it?
Can you please provide an example on how to just iterate over multiple objs inside the array and print the "name" and "age"?

Thank you in advance!

@ibireme
Copy link
Owner

ibireme commented Oct 20, 2021

Try this:

const char *str = "{\"people\": [ {\"name\":\"Alice\", \"age\": 40}, {\"name\": \"John\", \"age\": 38} ]}";
yyjson_doc *doc = yyjson_read(str, strlen(str), 0);
yyjson_val *root = yyjson_doc_get_root(doc);
yyjson_val *people_arr = yyjson_obj_get(root, "people");

yyjson_arr_iter people_iter;
yyjson_arr_iter_init(people_arr, &people_iter);
yyjson_val *one;
while ((one = yyjson_arr_iter_next(&people_iter))) {
    printf("------\n");
    printf("name: %s\n", yyjson_get_str(yyjson_obj_get(one, "name")));
    printf("age:  %d\n", yyjson_get_int(yyjson_obj_get(one, "age")));
}

yyjson_doc_free(doc);

Maybe I should add more sample code.

@ghost
Copy link
Author

ghost commented Oct 20, 2021

Thank you!

Just one follow-up question, does your library provide any function to make the conversion from:

{"people": [ {"name":"Alice", "age": 40}, {"name": "John", "age": 38} ]}

To:

"{\"people\": [ {\"name\":\"Alice\", \"age\": 40}, {\"name\": \"John\", \"age\": 38} ]}"

as the data that I am working with (from files and web requests) appears to be in the former form.

Thank you once again for your help!

@ghost
Copy link
Author

ghost commented Oct 20, 2021

Second (and last) follow-up question.

On the previous example, assume that "age" can either be an integer or a float.
I have tried

yyjson_get_real(yyjson_obj_get(one, "age")
and it only works if it is a floating point and returns zero if age is an integer.

Now assume that you would always want to store "age" as a float/double, would it be necessary to check both functions yyjson_get_real, yyjson_get_int, and convert the result of the latter to (double) or is it possible to parse directly to double, even if the value is an int?

Thank you. :)

@ibireme
Copy link
Owner

ibireme commented Oct 21, 2021

  1. "{\"people":...}" is an escaped c string literal for c source code: https://en.wikipedia.org/wiki/Escape_sequences_in_C
    If you have the original JSON data, just pass it directly to yyjson.

  2. If the type of a value is unknown, you should check the type before:
    double num = yyjson_is_int(val) ? yyjson_get_int(val) : yyjson_get_real(val);

@ghost
Copy link
Author

ghost commented Oct 21, 2021

Fantastic.
Thank you very much for the support.
Please feel free to close this issue.

@ibireme ibireme closed this as completed Oct 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant