Comments (5)
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.
from yyjson.
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!
from yyjson.
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. :)
from yyjson.
-
"{\"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. -
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);
from yyjson.
Fantastic.
Thank you very much for the support.
Please feel free to close this issue.
from yyjson.
Related Issues (20)
- Move ownership of a user allocated C string to yyjson object HOT 6
- Support `yyjson_mut_obj_add_obj` & `yyjson_mut_obj_add_arr` like `yyjson_mut_arr_add_*` HOT 6
- mem leak HOT 1
- Static Code Analysis: Identical sub-expressions on both sides of operator "<<" HOT 1
- Shared libraries need to be properly versioned on Linux HOT 1
- Json files are not newline terminated when pretty printing HOT 3
- memory issue for recursive function HOT 7
- yyjson_ptr_get_sint() returns false for positive integer HOT 5
- How to change the object key in mutable object? HOT 2
- error: -Wno-implicit-int and -Wno-implicit-function-declaration detected HOT 2
- Support Schema validator HOT 1
- A character make fail the JSON (empty JSON) HOT 1
- Does yyjson have parameters to set decimal places? HOT 4
- Discussion / Feedback request: Incorporating bytearray type HOT 8
- incorrectly formatted bool fields. HOT 3
- I need to merge multiple JSON strings HOT 1
- Is there a method to store the array of primitive types? HOT 4
- Release life cycle HOT 3
- Remove static qualifiers to support wrapping in other languages HOT 5
- Report row and line number when generating errors, instead of an absolute position HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from yyjson.