簡體   English   中英

在C中使用libcURL訪問google Places API數據時打印出的網格化結果

[英]a meshed-up result printed out while accessing google Places API data using libcURL in C

我在使用C語言的libcURL從google place API(文本搜索請求, https://developers.google.com/places/documentation/search )將HTTP數據獲取到char *時,陷入了問題。

這是我的代碼的一部分:

#include <stdio.h>
#include <curl/curl.h>

#define RESULT_SIZE 1024

char result[RESULT_SIZE];

static size_t write_data(char* buf, size_t size, size_t count, void *stream) {
    int c;
    for(c=0;c<size*count;c++)
        result[c]=buf[c];
    return size*count;
}

int main(void) {
    int i;
    char* query[300] = "";
    CURL *curl;
    CURLcode res;

    //initializing..
    for(i=0;i<RESULT_SIZE;i++)
        result[i]=0;
    curl_global_init(CURL_GLOBAL_ALL);
    curl = curl_easy_init();

    // make query sentence
    strcat(query,"https://maps.googleapis.com/maps/api/place/textsearc/json?query=40.690043,-74.045062&key=<myGoogleAPIkey>");

    curl_easy_setopt(curl, CURLOPT_URL, query);
    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);

    res = curl_easy_perform(curl);

    printf("%s\n", result");
}

我想要的結果是:

{
   "html_attributions" : [],
   "results" : [
      {
         "formatted_address" : "Statue of Liberty National Monument, Statue Of Liberty, 292 Madison Ave, New York, NY 10017, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.69004950,
               "lng" : -74.04506750
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 40.69850950,
                  "lng" : -74.02906010
               },
               "southwest" : {
                  "lat" : 40.68158840,
                  "lng" : -74.06107489999999
               }
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "c42384e74cfe05bd24d144b520b95cab496b9593",
         "name" : "Statue of Liberty National Monument",
         "reference" : "CpQBiwAAAJB0nFYotkps-eKyoe4WdSHfbKjGcQKSslzCtxpYrmZXS4-0jEXXz_iQV5ngCostzgcMQwrzLFFoJuY8OTlxltNs0GEVuslLNUjPSw4qMHKA-UIZ4_uPAbGONiLaYK5rW4UE--Ff0CTkaRTWsykQoEhJi8Vtekk1RgaexuJgjrsrWOf8HqCsw_mTXa24fqzYUhIQj-AwBAZy08dB0Y8jUZ0bERoUpKEkf-KXVCGrRGayir9y1KOnBpA",
         "types" : [ "point_of_interest", "establishment" ]
      }
   ],
   "status" : "OK"
}

但是我的程序打印出來了:

PUIJhMF-RoUWJ53xFJbynmmPscOUglzfinXnoE",
     "types" : [ "point_of_interest", "establishment" ]
  }
   ],
   "status" : "OK"
}
iberty, 292 Madison Ave, New York, NY 10017, USA",
         "geometry" : {
            "location" : {
               "lat" : 40.69004950,
               "lng" : -74.04506750
            },
            "viewport" : {
               "northeast" : {
                  "lat" : 40.69850950,
                  "lng" : -74.02906010
               },
               "southwest" : {
                  "lat" : 40.68158840,
                  "lng" : -74.06107489999999
               }
            }
         },
         "icon" : "http://maps.gstatic.com/mapfiles/place_api/icons/generic_business-71.png",
         "id" : "c42384e74cfe05bd24d144b520b95cab496b9593",
         "name" : "Statue of Liberty National Monument",
         "reference" : "CpQBigAAAIIwNFStfq0qjoQp-alxRtjFkaonf462dz_qHebtyT8zwc-WrtGdKmEWF-s05xhZasmbfG2wjqsWR_9UBvWfvo5hXTRTEhFCvD4AxdfJ5sWZIJ1_Rp2RpPdibI0aN30vkjXjATkyrv3W7T8pqZZnEU-zGvO_PhtZh05QJbp0Tw4sxKjgUBrfzw2sKCVXpUXQWBIQ_6rPxztrs4rSI

...這樣的混搭字符串

難道我做錯了什么? 那么我如何獲取完整的JSON類型數據呢? 還是比使用C編碼更好的建議? (實際上,我已經厭倦了在C .. :()中使用字符串格式。

提前致謝。

嘗試先解決此錯誤:

printf("%s\n", result");

應該

printf("%s\n", result);

另外... RESULT_SIZE太小。 增加它。

加號...每當您獲得一個數據塊並調用write函數時, result數組將從第一個字節開始覆蓋,而不是連接結果

暫無
暫無

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

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