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

Can't parse json array #53

Open
ssaguiar opened this issue Nov 13, 2019 · 1 comment
Open

Can't parse json array #53

ssaguiar opened this issue Nov 13, 2019 · 1 comment

Comments

@ssaguiar
Copy link

I receive a json object, wich has several informations and also an array of interfaces wuth several informations for each of them.
The array can be of any size (from one element to 4, 5 or even 6 elements).

The json is this:

{"status": "1", "message": "No Errors", "channel": "TEST", "writes": "481725079297", "cpu": "1.22", "memory": "403440", "ports": {"enp7s0f1": [{"byteIn": "118585715528669","byteOut": "1094749498510","packetIn": "78618393261","packetOut": "8078419574"}],"enp6s0f1": [{"byteIn": "0","byteOut": "0","packetIn": "0","packetOut": "0"}],"enp3s0": [{"byteIn": "0","byteOut": "0","packetIn": "0","packetOut": "0"}],"enp7s0f0": [{"byteIn": "0","byteOut": "0","packetIn": "0","packetOut": "0"}],"enp6s0f0": [{"byteIn": "0","byteOut": "0","packetIn": "0","packetOut": "0"}],"lo": [{"byteIn": "1532650647","byteOut": "1532650647","packetIn": "5444282","packetOut": "544428"}]}}

I need to parse the ports array.

So, I did this way:

	json_scanf(buf, strlen(buf), "{ status: %Q, message: %Q, channel: %Q , writes: %Q, cpu: %Q, memory: %Q }", &status, &message, &channel, &writes, &cpu, &mem);
	printf("status: %s\nmessage: %s\nchannel: %s\nwrites: %s\ncpu: %s\nmem: %s\n", status, message, channel, writes, cpu, mem);		

static void scan_array2(const char *str, int len, void *user_data) {
struct json_token t;
int i;
float bytein;

printf("Parsing array: %.*s\n", len, str);

for (i = 0; json_scanf_array_elem(str, len, "", i, &t) > 0; i++) {
	printf("Index %d, token %.*s\n", i, t.len, t.ptr);
	json_scanf(t.ptr, t.len, "{byteIn: %f}", &bytein);
	printf("Bytes in: %f\n", bytein);
}

}

The result is:

status: 1
message: No Errors
channel: TEST
writes: 481725079297
cpu: 1.22
mem: 403440

Parsing array: {"enp7s0f1": [{"byteIn": "118585715528669","byteOut": "1094749498510","packetIn": "78618393261","packetOut": "8078419574"}],"enp6s0f1": [{"byteIn": "0","byteOut": "0","packetIn": "0","packetOut": "0"}],"enp3s0": [{"byteIn": "0","byteOut": "0","packetIn": "0","packetOut": "0"}],"enp7s0f0": [{"byteIn": "0","byteOut": "0","packetIn": "0","packetOut": "0"}],"enp6s0f0": [{"byteIn": "0","byteOut": "0","packetIn": "0","packetOut": "0"}],"lo": [{"byteIn": "1532650647","byteOut": "1532650647","packetIn": "5444282","packetOut": "544428"}]}

It doesn't parse the array, even entering the method scan_array2.

Can you help to find how thi scan be done? The examples are not clear, for me.

Thnaks

@nliviu
Copy link

nliviu commented Nov 15, 2019

You need the name of the interface in the json_scanf_array_elem call.
Something like json_scanf_array_elem(str, len, ".enp7s0f1", i, &t), supposing you call json_scanf(buf, strlen(buf), "{ports:[%M]}", scan_array2, NULL);

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