问题描述
system("curl -d @gateway_req.json -H \"Content-Type: application/json\" -i \"https://www.googleapis.com/geolocation/v1/geolocate?key=MY_API_KEY");
返回如下,
HTTP/1.1 200 OK
Content-Type: application/json; charset=UTF-8
vary: X-Origin
vary: Referer
Date: Fri,04 Sep 2020 16:15:39 GMT
Server: scaffolding on HTTPServer2
Cache-Control: private
X-XSS-Protection: 0
x-frame-options: SAMEORIGIN
X-Content-Type-Options: nosniff
Alt-Svc: h3-29=":443"; ma=2592000,h3-27=":443"; ma=2592000,h3-T051=":443"; ma=25 92000,h3-T050=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2 592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
Accept-Ranges: none
vary: Origin,Accept-Encoding
transfer-encoding: chunked
{
"location": {
"lat": 20.7732224,"lng": -13.990144
},"accuracy": 1030
}
我想分别解码lat
和lng
值并将其存储在数组中。
我已经安装了jansson lib,但不确定如何使用它。你能给我一些想法怎么做吗?
解决方法
首先,您需要找到有效载荷的起点。这是在找到空行之后。
然后,您需要通过json_loads
解析字符串,您会收到一个JSON根对象。您要查找的数据存储在嵌入式对象"location"
中,您可以通过json_object_get
对其进行检索。
然后,您可以解析内容。这可以通过两种方式完成:
- 您可以使用格式字符串来解析该对象的成员,或者
- 您可以获取位置对象的子对象并检索每个对象的值。
完成工作后,您需要减少参考计数器的数量,以释放对象。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <jansson.h>
// gcc -o json-test `pkg-config --cflags jansson` json.c `pkg-config --libs jansson`
int main(void)
{
FILE *input = fopen("curl-data.txt","rt");
if (input == NULL) {
fprintf(stderr,"Cannot open curl-data.txt.\n");
return 1;
}
// sample data has 878 bytes.
char buffer[1024];
int num = fread(buffer,1,sizeof(buffer)-1,input);
buffer[num]=0;
// In this example the payload format is known and we can directly search
// for the start. In HTTP packets header and payload are separated by an empty
// line and that can be used to search for start of payload.
char *data_start = strstr(buffer,"{");
if (data_start == NULL) {
fprintf(stderr,"did not find start of payload.\n");
return 1;
}
printf("Payload: ###\n%s\n###\n",data_start);
json_error_t err;
json_t *root = json_loads(data_start,&err);
if (root == NULL) {
fprintf(stderr,"Failed to parse input string: %s\n",err.text);
return 1;
}
if (!json_is_object(root)) {
fprintf(stderr,"root is not an object\n");
json_decref(root);
return 1;
}
json_t *location = json_object_get(root,"location");
if (location == NULL || !json_is_object(location)) {
fprintf(stderr,"Location element not found or not an object\n");
json_decref(root);
return 1;
}
double lat,lng;
// alternative 1:
int res = json_unpack(location,"{s:f,s:f}","lat",&lat,"lng",&lng);
if (res != 0) {
fprintf(stderr,"lat/lng elements not found\n");
json_decref(root);
return 1;
}
printf("1) lat: %f; lng: %f\n",lat,lng);
// alternative 2:
json_t *latval = json_object_get(location,"lat");
if (latval == NULL || !json_is_real(latval)) {
fprintf(stderr,"lat element not found or not a number\n");
json_decref(root);
return 1;
}
json_t *lngval = json_object_get(location,"lng");
if (lngval == NULL || !json_is_real(lngval)) {
fprintf(stderr,"lng element not found or not a number\n");
json_decref(root);
return 1;
}
lat = json_number_value(latval);
lng = json_number_value(lngval);
printf("2) lat: %f; lng: %f\n",lng);
json_decref(root);
return 0;
}
无论您是否还需要减少其他2个对象的引用计数,我都需要深入研究手册。