-
Notifications
You must be signed in to change notification settings - Fork 260
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
Comments
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. |
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! |
Second (and last) follow-up question. On the previous example, assume that "age" can either be an integer or a float.
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. :) |
|
Fantastic. |
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!
The text was updated successfully, but these errors were encountered: