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

json simple parse syntax #807

Closed
fallaha opened this issue Jan 15, 2023 · 1 comment
Closed

json simple parse syntax #807

fallaha opened this issue Jan 15, 2023 · 1 comment

Comments

@fallaha
Copy link

fallaha commented Jan 15, 2023

I develop some function for parse JSON easier.
assume the below json:

{
    "episodes" : [
        {
            "title" : "First Movie",
            "cover" : "https://example.com/banner1.jpg",
            "imdb_rate" : 3.4,
            "comments" : ["great", "not good", "best movie"]
        },
        {
            "title" : "Second Movie",
            "cover" : "https://example.com/banner2.jpg",
            "imdb_rate" : 6,
            "comments" : ["great", "not good", "best movie"]
        }
    ],
    "success" : true
}

you can easily access to title of Second Movie with this function

jsonp(obj, "episodes[].title", 1).string

or for get third comment of first movie you can

jsonp(obj, "eposides[].comments[]",0,2).string

for print all cover url with imdb rate you can:

for (int i = 0; i < jsonp_length(obj, "episodes[]"); i++)
{
    char* cover = jsonp(obj, "episodes[].cover", i).string;
    int rate = jsonp(obj, "episode[].imdb_rate", i).number;
    printf("url: %s, rate: %d", cover, rate);
}

can I pull request it to merge on json-c library?

@hawicz
Copy link
Member

hawicz commented Jan 15, 2023

We already have http://json-c.github.io/json-c/json-c-0.16/doc/html/json__pointer_8h.html, aka JSON Pointer, aka RFC6901

@hawicz hawicz closed this as completed Jan 15, 2023
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

2 participants