簡體   English   中英

如何從google api解碼json數據

[英]How to decode json data from google api

我在我的程序中使用以下代碼來獲取緯度和經度。

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
}

我想分別解碼latlng值並將其存儲在一個數組中。

我已經安裝了 jansson lib,但不確定如何使用它。 你能給我一些想法如何做到這一點嗎?

首先,您需要找到有效負載的開頭。 這是在發現空行之后。

然后你需要通過json_loads解析字符串,你會收到一個根 JSON 對象。 您要查找的數據存儲在嵌入對象"location" ,您可以通過json_object_get檢索該"location" 然后就可以解析內容了。 這可以通過兩種方式完成:

  • 您可以使用格式字符串來解析該對象的成員或
  • 您可以獲取位置對象的子對象並檢索每個對象的值。

完成工作后,您需要遞減引用計數器以釋放對象。

#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, 0, &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", lat, lng);

    json_decref(root);

    return 0;
}

我需要深入研究手冊,您是否還需要為其他 2 個對象減少 refcounter。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM