1

我对我的 c 代码有疑问,

我的代码在做什么?

  • 调用谷歌API“方向”
  • 得到一个包含 json 结构的大字符串“s”
  • 将我的字符串 s 写入文件

我的问题是什么?我想创建一个自动结构,因为我的结构总是不同的。例如,有时如果只有 200 或 1000 个坐标。

#include <stdio.h>
#include <curl/curl.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#define MAX 300


struct string {
  char *ptr;
  size_t len;
};


void init_string(struct string *s) {
  s->len = 0;
  s->ptr = malloc(s->len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "malloc() failed\n");
 
  }
  s->ptr[0] = '\0';
}




size_t writefunc(void *ptr, size_t size, size_t nmemb, struct string *s)
{
  size_t new_len = s->len + size*nmemb;
  s->ptr = realloc(s->ptr, new_len+1);
  if (s->ptr == NULL) {
    fprintf(stderr, "realloc() failed\n");
  }
  memcpy(s->ptr+s->len, ptr, size*nmemb);
  s->ptr[new_len] = '\0';
  s->len = new_len;

  return size*nmemb;
}


void getRoad(double doubleLatitude, double doubleLongitude,char *villeDestination[MAX]){
  
  printf("Function : getRoad\n");
  printf("parameters latitude -%f-\n",doubleLatitude);
  printf("parameters longitude -%f-\n",doubleLongitude);
  printf("parameters villeDestination -%s-\n",villeDestination);

  char URL_BASE[MAX],URL;
  

  // CONVERT DOUBLE TO STRING 
  char stringLatitude[50];
  char stringLongitude[50];

  snprintf(stringLatitude, 50, "%f",doubleLatitude);
  snprintf(stringLongitude, 50, "%f",doubleLongitude);


  // WE ARE BUILDING URL URL 
  strcpy (URL_BASE,"https://maps.googleapis.com/maps/api/directions/json?origin=");
  strcat(URL_BASE,stringLatitude);
  strcat(URL_BASE,",");
  strcat(URL_BASE,stringLongitude);
  strcat(URL_BASE,"&destination=");
  strcat(URL_BASE,villeDestination);
  strcat(URL_BASE,"&avoid=highways&mode=bicycling&key=AIzaSyBpj0XoMAHi8naH5k-S8mAr0nexwCQvv2g");

  // OUR URL, YOU CAN TEST IT IN FIREFOX ! 
  printf("%s\n",URL_BASE);

  CURL *curl;
  CURLcode res;

  curl_global_init(CURL_GLOBAL_DEFAULT);

  curl = curl_easy_init();

  if(curl) {

    struct string s;
    init_string(&s);

    curl_easy_setopt(curl, CURLOPT_URL, URL_BASE);

  #ifdef SKIP_PEER_VERIFICATION  
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
  #endif

  #ifdef SKIP_HOSTNAME_VERIFICATION
    curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  #endif

    curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
    curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);

  res = curl_easy_perform(curl);
   
  printf("%s\n", s.ptr);//DEBUG



  // OPEN A FILE AND WRITE DATA 
  FILE *fptr;
  fptr = fopen("dataJson.json", "w");
  if (fptr == NULL) {
    printf("Error!");
    exit(1);
  }
  fprintf(fptr, "%s", s.ptr);
  fclose(fptr);


  //printf("%s\n",curl_easy_perform(curl));//DEBUG
  if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res));
    curl_easy_cleanup(curl);
  }
  curl_global_cleanup();
}

  /*
   * Exemple type adepart avec des coordonnées 
   * 
   * origin 50.642782,2.833267
   * destination paris
   *
   * https://maps.googleapis.com/maps/api/directions/json?origin=50.642782,2.833267&destination=paris&avoid=highways&mode=bicycling&key=AIzaSyBpj0XoMAHi8naH5k-S8mAr0nexwCQvv2g
  */


int main(void){
  
  //getCoordinate();
  // Coord depart - ville destination
  getRoad(50.642782,2.833267,"nieppe");

  return 0;
}

在此处输入图像描述

4

1 回答 1

0

我不知道如何像这样访问我的大字符串中的数据 { "geocoded_waypoints" : [ { "geocoder_status" : "OK", "place_id" : "ChIJbRcWwsZMFkcRcXvXdyuxSyw", "types" : [ "street_address" ] }, { “geocoder_status”:“OK”,“place_id”:“ChIJD7fiBh9u5kcRYJSMaMOCCwQ”,“types”:[“locality”,“political”]}],“routes”:[{“bounds”:{“northeast”:{“lat " : 52.2236303, "lng" : 20.203097 }

您似乎想要一个 JSON 实现来解析 JSON 格式的字符串。也许您的系统上已经有了JSON-C;在这种情况下,您只需将您的程序链接到-ljson-c,您可以使用例如这些调用:

#include <json-c/json.h>
...
    struct json_object *obj = json_tokener_parse(s.ptr);
    if (!obj) { fputs("json_tokener_parse failed\n", stderr); return; }

    struct json_object *way = json_object_object_get(obj, "geocoded_waypoints");
    if (!way) { fputs("no geocoded_waypoints\n", stderr); return; }

    size_t len = json_object_array_length(way);
    printf("There are %zd waypoints.\n", len);
    for (size_t idx = 0; idx < len; ++idx)
    {   printf("waypoint %zd:\n", idx);
        struct json_object *point = json_object_array_get_idx(way, idx);
        printf("geocoder_status is %s\n", json_object_get_string(json_object_object_get(point, "geocoder_status")));
        printf("place_id is %s\n",        json_object_get_string(json_object_object_get(point, "place_id")));
        printf("types is %s\n",           json_object_get_string(json_object_object_get(point, "types")));
    }

    struct json_object *routes = json_object_object_get(obj, "routes");
    if (!routes) { fputs("no routes\n", stderr); return; }

    size_t routeslen = json_object_array_length(routes);
    printf("There are %zd routes.\n", routeslen);
    for (size_t idx = 0; idx < routeslen; ++idx)
    {   printf("route %zd:\n", idx);
        struct json_object *route = json_object_array_get_idx(routes, idx);
        printf("bounds is %s\n", json_object_get_string(json_object_object_get(route, "bounds")));
    }
于 2021-03-02T19:21:51.217 回答