微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

API Google JSON 到 C

如何解决API Google JSON 到 C

我对我的 c 代码有疑问,

我的代码在做什么?

我的问题是什么? 我想创建一个自动结构,因为我的结构总是不同的。例如有时如果只有 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,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;
}

enter image description here

解决方法

我不知道如何像这样访问大字符串中的数据 { "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","place_id")));
        printf("types is %s\n","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")));
    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。