json-c

Parsing JSON array in C

ぐ巨炮叔叔 提交于 2021-02-10 15:01:26
问题 I have the following JSON returned from the server, and I'm trying to access to the Values (timestamp/data): { "queries": [ { "sample_size": 1, "results": [ { "name": "data", "group_by": [ { "name": "type", "type": "number" } ], "tags": { "hostname": [ "host" ] }, "values": [ [ 1438775895302, 143 ] ] } ] } ] } I am using json-c, and am using slightly modified version of the complete json parser. However I keep getting a segmentation fault (core dumped) when attempting to access the values

Parsing JSON array in C

喜欢而已 提交于 2021-02-10 14:58:41
问题 I have the following JSON returned from the server, and I'm trying to access to the Values (timestamp/data): { "queries": [ { "sample_size": 1, "results": [ { "name": "data", "group_by": [ { "name": "type", "type": "number" } ], "tags": { "hostname": [ "host" ] }, "values": [ [ 1438775895302, 143 ] ] } ] } ] } I am using json-c, and am using slightly modified version of the complete json parser. However I keep getting a segmentation fault (core dumped) when attempting to access the values

Traversing through and modifying a C json string

*爱你&永不变心* 提交于 2021-02-04 08:28:18
问题 What's the correct way to traverse and modify a JSON string in C? Specifically, I have a string, body_buf. When printed out print("length: %d\n%.*s\n", body_len, body_len, body_buf); It looks like this: length: 113 {"field1":"something","whatever":10,"description":"body","id":"random","__oh__":{"session":"12345678jhgfdrtyui"}} Another more complicated body_buf may look like this: {"status":1,"query":{},"proc":{"memory":{"total":17177939968,"cmax":18363625472,"amax":20000000000},"cpu":{"cores"

Parsing deeply nested JSON key using json-c

十年热恋 提交于 2021-01-28 15:55:00
问题 I'm using the json-c library, and after reviewing the documentation, I couldn't find a way to get at a deeply nested key/value without using a bunch of loops, here's what I've tried: json_object_object_foreach(json_data_obj, key, val) { printf("KEY:%s\t VAL:%s\n", key, json_object_to_json_string(val)); /* TODO: Traverse the JSON * "results" => "channel" => "item" => "condition" => "temp" */ } Here's the output: KEY:query VAL:{ "count": 1, "created": "2015-04-10T06:05:12Z", "lang": "en-US",

Parsing deeply nested JSON key using json-c

旧时模样 提交于 2021-01-28 15:38:29
问题 I'm using the json-c library, and after reviewing the documentation, I couldn't find a way to get at a deeply nested key/value without using a bunch of loops, here's what I've tried: json_object_object_foreach(json_data_obj, key, val) { printf("KEY:%s\t VAL:%s\n", key, json_object_to_json_string(val)); /* TODO: Traverse the JSON * "results" => "channel" => "item" => "condition" => "temp" */ } Here's the output: KEY:query VAL:{ "count": 1, "created": "2015-04-10T06:05:12Z", "lang": "en-US",

json-c string with “/” character

梦想与她 提交于 2020-08-07 18:04:39
问题 When my program saves something in json like this: json_object_object_add(jObj_my, "cats/dogs", json_object_new_double(cats/dogs)); the result in the .json file is: "cats\/dogs" : some_double_number How can i avoid it to print "\/" instead of "/" ? 回答1: The json-c library's code in its GitHub repository has a flag to make escaping of / optional. If you do not want the generated string to escape this, use the JSON_C_TO_STRING_NOSLASHESCAPE flag, like this: #include <stdio.h> #include <json.h>

json-c string with “/” character

℡╲_俬逩灬. 提交于 2020-08-07 18:03:55
问题 When my program saves something in json like this: json_object_object_add(jObj_my, "cats/dogs", json_object_new_double(cats/dogs)); the result in the .json file is: "cats\/dogs" : some_double_number How can i avoid it to print "\/" instead of "/" ? 回答1: The json-c library's code in its GitHub repository has a flag to make escaping of / optional. If you do not want the generated string to escape this, use the JSON_C_TO_STRING_NOSLASHESCAPE flag, like this: #include <stdio.h> #include <json.h>

json-c string with “/” character

三世轮回 提交于 2020-08-07 18:03:41
问题 When my program saves something in json like this: json_object_object_add(jObj_my, "cats/dogs", json_object_new_double(cats/dogs)); the result in the .json file is: "cats\/dogs" : some_double_number How can i avoid it to print "\/" instead of "/" ? 回答1: The json-c library's code in its GitHub repository has a flag to make escaping of / optional. If you do not want the generated string to escape this, use the JSON_C_TO_STRING_NOSLASHESCAPE flag, like this: #include <stdio.h> #include <json.h>

json-c string with “/” character

十年热恋 提交于 2020-08-07 18:01:05
问题 When my program saves something in json like this: json_object_object_add(jObj_my, "cats/dogs", json_object_new_double(cats/dogs)); the result in the .json file is: "cats\/dogs" : some_double_number How can i avoid it to print "\/" instead of "/" ? 回答1: The json-c library's code in its GitHub repository has a flag to make escaping of / optional. If you do not want the generated string to escape this, use the JSON_C_TO_STRING_NOSLASHESCAPE flag, like this: #include <stdio.h> #include <json.h>

Valid json check in C language

帅比萌擦擦* 提交于 2020-05-28 11:56:21
问题 I have searched a lot in Google. I want to programmatically check if the string below is valid JSON in C. How can I do that? (I am currently using the json-c library.) char * string = "{ number1 : 100, number2 : 10, files : [ c , c++, java,PHP,java,PHP ], random: [123567876523333,908,988] }"; The library has no function to check if the string is valid JSON. 回答1: You can use the function json_tokener_parse(const char *str) , it returns NULL if parsing fails. See here for details. 来源: https:/