2017-05-17 2 views
0

Je possède ce assez grand JSON, que je nourris dans un tableau const char comme ceci:Rapidjson Erreur d'analyse: encodage non valide dans la chaîne (207)

const char tl_json[] = "[" 
     "{" 
     "\"created_at\": \"Thu Apr 06 15:28:43 +0000 2017\"," 
"\"id\": 850007368138018817," 
"\"id_str\": \"850007368138018817\"," 
"\"text\": \"RT @TwitterDev: 1/ Today we're sharing our vision for the future of the Twitter API platform!\nhttps://twitter.com"," 
"\"truncated\": false," 
"\"entities\": {" 
"\"hashtags\": []," 
"\"symbols\": []," 
"\"user_mentions\": [" 
"{" 
"\"screen_name\": \"TwitterDev\"," 
"\"name\": \"TwitterDev\"," 
"\"id\": 2244994945," 
"\"id_str\": \"2244994945\"," 
"\"indices\": [" 
"3," 
"14" 
"]" 
"}" 
"]," 
"\"urls\": [" 
"{" 
"\"url\": \"https://twitter.com\"," 
"\"expanded_url\": \"https://cards.twitter.com/cards/18ce53wgo4h/3xo1c\"," 
"\"display_url\": \"cards.twitter.com/cards/18ce53wg\"," 
"\"indices\": [" 
"94," 
"117" 
"]" 
"}" 
"]" 
"}," 
"\"source\": \"<a href='http://twitter.com' rel='nofollow'>Twitter Web Client</a>\"," 
"\"in_reply_to_status_id\": null," 
"\"in_reply_to_status_id_str\": null," 
"\"in_reply_to_user_id\": null," 
"\"in_reply_to_user_id_str\": null," 
"\"in_reply_to_screen_name\": null," 
"\"user\": {" 
"\"id\": 6253282," 
"\"id_str\": \"6253282\"," 
"\"name\": \"Twitter API\"," 
"\"screen_name\": \"twitterapi\"," 
"\"location\": \"San Francisco, CA\"," 
"\"description\": \"The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.\"," 
"\"url\": \"https://dev.twitter.com\"," 
"\"entities\": {" 
"\"url\": {" 
"\"urls\": [" 
"{" 
"\"url\": \"https://dev.twitter.com\"," 
"\"expanded_url\": \"https://dev.twitter.com\"," 
"\"display_url\": \"dev.twitter.com\"," 
"\"indices\": [" 
"0," 
"22" 
"]" 
"}" 
"]" 
"}," 
"\"description\": {" 
"\"urls\": []" 
"}" 
"}," 
"\"protected\": false," 
"\"followers_count\": 6172353," 
"\"friends_count\": 46," 
"\"listed_count\": 13091," 
"\"created_at\": \"Wed May 23 06:01:13 +0000 2007\"," 
"\"favourites_count\": 26," 
"\"utc_offset\": -25200," 
"\"time_zone\": \"Pacific Time (US & Canada)\"," 
"\"geo_enabled\": true," 
"\"verified\": true," 
"\"statuses_count\": 3583," 
"\"lang\": \"en\"," 
"\"contributors_enabled\": false," 
"\"is_translator\": false," 
"\"is_translation_enabled\": false," 
"\"profile_background_color\": \"C0DEED\"," 
"\"profile_background_image_url\": \"http://pbs.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png\"," 
"\"profile_background_image_url_https\": \"https://pbs.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png\"," 
"\"profile_background_tile\": true," 
"\"profile_image_url\": \"http://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png\"," 
"\"profile_image_url_https\": \"https://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png\"," 
"\"profile_banner_url\": \"https://pbs.twimg.com/profile_banners/6253282/1431474710\"," 
"\"profile_link_color\": \"0084B4\"," 
"\"profile_sidebar_border_color\": \"C0DEED\"," 
"\"profile_sidebar_fill_color\": \"DDEEF6\"," 
"\"profile_text_color\": \"333333\"," 
"\"profile_use_background_image\": true," 
"\"has_extended_profile\": false," 
"\"default_profile\": false," 
"\"default_profile_image\": false," 
"\"following\": true," 
"\"follow_request_sent\": false," 
"\"notifications\": false," 
"\"translator_type\": \"regular\"" 
"}," 
"\"geo\": null," 
"\"coordinates\": null," 
"\"place\": null," 
"\"contributors\": null," 
"\"retweeted_status\": {" 
"\"created_at\": \"Thu Apr 06 15:24:15 +0000 2017\"," 
"\"id\": 850006245121695744," 
"\"id_str\": \"850006245121695744\"," 
"\"text\": \"1/ Today blank sharing our vision for the future of the Twitter API platform!\nhttps://blog.twitter.com/2017/building-the-future-of-the-twitter-api-platform\"," 
"\"truncated\": false," 
"\"entities\": {" 
"\"hashtags\": []," 
"\"symbols\": []," 
"\"user_mentions\": []," 
"\"urls\": [" 
"{" 
"\"url\": \"https://twitter.com\"," 
"\"expanded_url\": \"https://cards.twitter.com/cards/18ce53wgo4h/3xo1c\"," 
"\"display_url\": \"cards.twitter.com/cards/18ce53wg\"," 
"\"indices\": [" 
"78," 
"101" 
"]" 
"}" 
"]" 
"}," 
"\"source\": \"<a href='http://twitter.com' rel='nofollow'>Twitter Web Client</a>\"," 
"\"in_reply_to_status_id\": null," 
"\"in_reply_to_status_id_str\": null," 
"\"in_reply_to_user_id\": null," 
"\"in_reply_to_user_id_str\": null," 
"\"in_reply_to_screen_name\": null," 
"\"user\": {" 
"\"id\": 2244994945," 
"\"id_str\": \"2244994945\"," 
"\"name\": \"TwitterDev\"," 
"\"screen_name\": \"TwitterDev\"," 
"\"location\": \"Internet\"," 
"\"description\": \"Your official source for Twitter Platform news, updates & events. Need technical help? Visit https://twitter.com #TapIntoTwitter\"," 
"\"url\": \"https://twitter.com\"," 
"\"entities\": {" 
"\"url\": {" 
"\"urls\": [" 
"{" 
"\"url\": \"https://twitter.com\"," 
"\"expanded_url\": \"https://dev.twitter.com/\"," 
"\"display_url\": \"dev.twitter.com\"," 
"\"indices\": [" 
"0," 
"23" 
"]" 
"}" 
"]" 
"}," 
"\"description\": {" 
"\"urls\": [" 
"{" 
"\"url\": \"https://twitter.com\"," 
"\"expanded_url\": \"https://twittercommunity.com/\"," 
"\"display_url\": \"twittercommunity.com\"," 
"\"indices\": [" 
"93," 
"116" 
"]" 
"}" 
"]" 
"}" 
"}," 
"\"protected\": false," 
"\"followers_count\": 465425," 
"\"friends_count\": 1523," 
"\"listed_count\": 1168," 
"\"created_at\": \"Sat Dec 14 04:35:55 +0000 2013\"," 
"\"favourites_count\": 2098," 
"\"utc_offset\": -25200," 
"\"time_zone\": \"Pacific Time (US & Canada)\"," 
"\"geo_enabled\": true," 
"\"verified\": true," 
"\"statuses_count\": 3031," 
"\"lang\": \"en\"," 
"\"contributors_enabled\": false," 
"\"is_translator\": false," 
"\"is_translation_enabled\": false," 
"\"profile_background_color\": \"FFFFFF\"," 
"\"profile_background_image_url\": \"http://abs.twimg.com/images/themes/theme1/bg.png\"," 
"\"profile_background_image_url_https\": \"https://abs.twimg.com/images/themes/theme1/bg.png\"," 
"\"profile_background_tile\": false," 
"\"profile_image_url\": \"http://pbs.twimg.com/profile_images/530814764687949824/npQQVkq8_normal.png\"," 
"\"profile_image_url_https\": \"https://pbs.twimg.com/profile_images/530814764687949824/npQQVkq8_normal.png\"," 
"\"profile_banner_url\": \"https://pbs.twimg.com/profile_banners/2244994945/1396995246\"," 
"\"profile_link_color\": \"0084B4\"," 
"\"profile_sidebar_border_color\": \"FFFFFF\"," 
"\"profile_sidebar_fill_color\": \"DDEEF6\"," 
"\"profile_text_color\": \"333333\"," 
"\"profile_use_background_image\": false," 
"\"has_extended_profile\": false," 
"\"default_profile\": false," 
"\"default_profile_image\": false," 
"\"following\": true," 
"\"follow_request_sent\": false," 
"\"notifications\": false," 
"\"translator_type\": \"regular\"" 
"}," 
"\"geo\": null," 
"\"coordinates\": null," 
"\"place\": null," 
"\"contributors\": null," 
"\"is_quote_status\": false," 
"\"retweet_count\": 284," 
"\"favorite_count\": 399," 
"\"favorited\": false," 
"\"retweeted\": false," 
"\"possibly_sensitive\": false," 
"\"lang\": \"en\"" 
"}," 
"\"is_quote_status\": false," 
"\"retweet_count\": 284," 
"\"favorite_count\": 0," 
"\"favorited\": false," 
"\"retweeted\": false," 
"\"possibly_sensitive\": false," 
"\"lang\": \"en\"" 
     "}]"; 

Lorsque je tente de l'analyser comme ceci:

char tl_buffer[sizeof(tl_json)]; 
     memcpy(tl_buffer, tl_json, sizeof(tl_json)); 
     ParseResult ok = tl_document.ParseInsitu(tl_buffer); 
     if (!ok) { 
     fprintf(stderr, "JSON parse error: %s (%u)", 
     GetParseError_En(ok.Code()), ok.Offset()); 
     exit(EXIT_FAILURE); 
     } 

je reçois l'erreur suivante:

JSON parse error: Invalid encoding in string. (207) 

Et je ne sais pas quel est le problème avec mon JSON b/c je copiais & collé de la Twitter API json (it's the json under "Example Response")

Quand je copié et collé, j'ai remarqué le ' dans « nous sommes » était un caractère différent de celui du ' normal. J'ai également corrigé le ... b/c qui a également été copié et collé mal. B/c de ce que je ne reçois pas un avertissement sur ces caractères lorsque je compile dans mbed, mais cela n'a pas résolu mon problème principal: obtenir le json à analyser.

Toute aide serait appréciée!

Note: Pour voir JSON mieux j'ai inclus une version échancré:

"[" 
     "{" 
     "\"created_at\": \"Thu Apr 06 15:28:43 +0000 2017\"," 
"  \"id\": 850007368138018817," 
"  \"id_str\": \"850007368138018817\"," 
"  \"text\": \"RT @TwitterDev: 1/ Today we’re sharing our vision for the future of the Twitter API platform!\nhttps://blog.twitter.com/2017/building-the-future-of-the-twitter-api-platform\"," 
"  \"truncated\": false," 
"  \"entities\": {" 
"   \"hashtags\": []," 
"   \"symbols\": []," 
"   \"user_mentions\": [" 
"   {" 
"    \"screen_name\": \"TwitterDev\"," 
"    \"name\": \"TwitterDev\"," 
"    \"id\": 2244994945," 
"    \"id_str\": \"2244994945\"," 
"    \"indices\": [" 
"    3," 
"    14" 
"    ]" 
"   }" 
"   ]," 
"   \"urls\": [" 
"   {" 
"    \"url\": \"https://twitter.com\"," 
"    \"expanded_url\": \"https://cards.twitter.com/cards/18ce53wgo4h/3xo1c\"," 
"    \"display_url\": \"cards.twitter.com/cards/18ce53wg…\"," 
"    \"indices\": [" 
"    94," 
"    117" 
"    ]" 
"   }" 
"   ]" 
"  }," 
"  \"source\": \"<a href='http://twitter.com' rel='nofollow'>Twitter Web Client</a>\"," 
"  \"in_reply_to_status_id\": null," 
"  \"in_reply_to_status_id_str\": null," 
"  \"in_reply_to_user_id\": null," 
"  \"in_reply_to_user_id_str\": null," 
"  \"in_reply_to_screen_name\": null," 
"  \"user\": {" 
"   \"id\": 6253282," 
"   \"id_str\": \"6253282\"," 
"   \"name\": \"Twitter API\"," 
"   \"screen_name\": \"twitterapi\"," 
"   \"location\": \"San Francisco, CA\"," 
"   \"description\": \"The Real Twitter API. I tweet about API changes, service issues and happily answer questions about Twitter and our API. Don't get an answer? It's on my website.\"," 
"   \"url\": \"https://twitter.com\"," 
"   \"entities\": {" 
"   \"url\": {" 
"    \"urls\": [" 
"    {" 
"     \"url\": \"https://twitter.com\"," 
"     \"expanded_url\": \"https://dev.twitter.com\"," 
"     \"display_url\": \"dev.twitter.com\"," 
"     \"indices\": [" 
"     0," 
"     22" 
"     ]" 
"    }" 
"    ]" 
"   }," 
"   \"description\": {" 
"    \"urls\": []" 
"   }" 
"   }," 
"   \"protected\": false," 
"   \"followers_count\": 6172353," 
"   \"friends_count\": 46," 
"   \"listed_count\": 13091," 
"   \"created_at\": \"Wed May 23 06:01:13 +0000 2007\"," 
"   \"favourites_count\": 26," 
"   \"utc_offset\": -25200," 
"   \"time_zone\": \"Pacific Time (US & Canada)\"," 
"   \"geo_enabled\": true," 
"   \"verified\": true," 
"   \"statuses_count\": 3583," 
"   \"lang\": \"en\"," 
"   \"contributors_enabled\": false," 
"   \"is_translator\": false," 
"   \"is_translation_enabled\": false," 
"   \"profile_background_color\": \"C0DEED\"," 
"   \"profile_background_image_url\": \"http://pbs.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png\"," 
"   \"profile_background_image_url_https\": \"https://pbs.twimg.com/profile_background_images/656927849/miyt9dpjz77sc0w3d4vj.png\"," 
"   \"profile_background_tile\": true," 
"   \"profile_image_url\": \"http://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png\"," 
"   \"profile_image_url_https\": \"https://pbs.twimg.com/profile_images/2284174872/7df3h38zabcvjylnyfe3_normal.png\"," 
"   \"profile_banner_url\": \"https://pbs.twimg.com/profile_banners/6253282/1431474710\"," 
"   \"profile_link_color\": \"0084B4\"," 
"   \"profile_sidebar_border_color\": \"C0DEED\"," 
"   \"profile_sidebar_fill_color\": \"DDEEF6\"," 
"   \"profile_text_color\": \"333333\"," 
"   \"profile_use_background_image\": true," 
"   \"has_extended_profile\": false," 
"   \"default_profile\": false," 
"   \"default_profile_image\": false," 
"   \"following\": true," 
"   \"follow_request_sent\": false," 
"   \"notifications\": false," 
"   \"translator_type\": \"regular\"" 
"  }," 
"  \"geo\": null," 
"  \"coordinates\": null," 
"  \"place\": null," 
"  \"contributors\": null," 
"  \"retweeted_status\": {" 
"   \"created_at\": \"Thu Apr 06 15:24:15 +0000 2017\"," 
"   \"id\": 850006245121695744," 
"   \"id_str\": \"850006245121695744\"," 
"   \"text\": \"1/ Today we’re sharing our vision for the future of the Twitter API platform!\nhttps://twitter.com\"," 
"   \"truncated\": false," 
"   \"entities\": {" 
"   \"hashtags\": []," 
"   \"symbols\": []," 
"   \"user_mentions\": []," 
"   \"urls\": [" 
"    {" 
"    \"url\": \"https://twitter.com\"," 
"    \"expanded_url\": \"https://cards.twitter.com/cards/18ce53wgo4h/3xo1c\"," 
"    \"display_url\": \"cards.twitter.com/cards/18ce53wg…\"," 
"    \"indices\": [" 
"     78," 
"     101" 
"    ]" 
"    }" 
"   ]" 
"   }," 
"   \"source\": \"<a href='http://twitter.com' rel='nofollow'>Twitter Web Client</a>\"," 
"   \"in_reply_to_status_id\": null," 
"   \"in_reply_to_status_id_str\": null," 
"   \"in_reply_to_user_id\": null," 
"   \"in_reply_to_user_id_str\": null," 
"   \"in_reply_to_screen_name\": null," 
"   \"user\": {" 
"   \"id\": 2244994945," 
"   \"id_str\": \"2244994945\"," 
"   \"name\": \"TwitterDev\"," 
"   \"screen_name\": \"TwitterDev\"," 
"   \"location\": \"Internet\"," 
"   \"description\": \"Your official source for Twitter Platform news, updates & events. Need technical help? Visit https://twitter.com ⌨️ #TapIntoTwitter\"," 
"   \"url\": \"https://twitter.com\"," 
"   \"entities\": {" 
"    \"url\": {" 
"    \"urls\": [" 
"     {" 
"     \"url\": \"https://twitter.com\"," 
"     \"expanded_url\": \"https://dev.twitter.com/\"," 
"     \"display_url\": \"dev.twitter.com\"," 
"     \"indices\": [" 
"      0," 
"      23" 
"     ]" 
"     }" 
"    ]" 
"    }," 
"    \"description\": {" 
"    \"urls\": [" 
"     {" 
"     \"url\": \"https://twitter.com\"," 
"     \"expanded_url\": \"https://twittercommunity.com/\"," 
"     \"display_url\": \"twittercommunity.com\"," 
"     \"indices\": [" 
"      93," 
"      116" 
"     ]" 
"     }" 
"    ]" 
"    }" 
"   }," 
"   \"protected\": false," 
"   \"followers_count\": 465425," 
"   \"friends_count\": 1523," 
"   \"listed_count\": 1168," 
"   \"created_at\": \"Sat Dec 14 04:35:55 +0000 2013\"," 
"   \"favourites_count\": 2098," 
"   \"utc_offset\": -25200," 
"   \"time_zone\": \"Pacific Time (US & Canada)\"," 
"   \"geo_enabled\": true," 
"   \"verified\": true," 
"   \"statuses_count\": 3031," 
"   \"lang\": \"en\"," 
"   \"contributors_enabled\": false," 
"   \"is_translator\": false," 
"   \"is_translation_enabled\": false," 
"   \"profile_background_color\": \"FFFFFF\"," 
"   \"profile_background_image_url\": \"http://abs.twimg.com/images/themes/theme1/bg.png\"," 
"   \"profile_background_image_url_https\": \"https://abs.twimg.com/images/themes/theme1/bg.png\"," 
"   \"profile_background_tile\": false," 
"   \"profile_image_url\": \"http://pbs.twimg.com/profile_images/530814764687949824/npQQVkq8_normal.png\"," 
"   \"profile_image_url_https\": \"https://pbs.twimg.com/profile_images/530814764687949824/npQQVkq8_normal.png\"," 
"   \"profile_banner_url\": \"https://pbs.twimg.com/profile_banners/2244994945/1396995246\"," 
"   \"profile_link_color\": \"0084B4\"," 
"   \"profile_sidebar_border_color\": \"FFFFFF\"," 
"   \"profile_sidebar_fill_color\": \"DDEEF6\"," 
"   \"profile_text_color\": \"333333\"," 
"   \"profile_use_background_image\": false," 
"   \"has_extended_profile\": false," 
"   \"default_profile\": false," 
"   \"default_profile_image\": false," 
"   \"following\": true," 
"   \"follow_request_sent\": false," 
"   \"notifications\": false," 
"   \"translator_type\": \"regular\"" 
"   }," 
"   \"geo\": null," 
"   \"coordinates\": null," 
"   \"place\": null," 
"   \"contributors\": null," 
"   \"is_quote_status\": false," 
"   \"retweet_count\": 284," 
"   \"favorite_count\": 399," 
"   \"favorited\": false," 
"   \"retweeted\": false," 
"   \"possibly_sensitive\": false," 
"   \"lang\": \"en\"" 
"  }," 
"  \"is_quote_status\": false," 
"  \"retweet_count\": 284," 
"  \"favorite_count\": 0," 
"  \"favorited\": false," 
"  \"retweeted\": false," 
"  \"possibly_sensitive\": false," 
"  \"lang\": \"en\"" 
     "}]"; 
+0

Le premier bloc de code dans votre question [n'est même pas valide C++] (http://coliru.stacked-crooked.com/a/9a1cba75c53986f2). – cdhowie

+0

@cdhowie que voulez-vous dire? Je sais que je peux faire une chaîne multiligne en C++ http://stackoverflow.com/questions/1135841/c-multiline-string-literal – 14wml

+0

Cliquez sur le lien et regardez la sortie du compilateur - vous avez oublié d'échapper un caractère '" ' – cdhowie

Répondre

1

Votre problème est que vous ne l'avez pas échapper à tous les antislash dans l'entrée. Pour cette raison, la séquence \n est interprétée dans votre compilateur C++ comme une séquence d'échappement pour le caractère de nouvelle ligne, mais JSON n'autorise pas les nouvelles lignes littérales dans les chaînes. Par exemple, essayez cette analyse syntaxique et vous aurez le même problème:

char const tl_json[] = "{\"a\":\"foo\nbar\"}"; 

Ce produit littéral chaîne suivante:

{"a":"foo 
bar"} 

Ce n'est pas JSON valide en raison de la nouvelle ligne intégrée dans la chaîne . JSON permet aux espaces blancs d'apparaître entre les jetons, mais une chaîne est un seul jeton et les retours à la ligne ne sont pas autorisés à y apparaître.

Vous devez échapper aux caractères backslash ainsi, de sorte que la chaîne C contient \n deux caractères - une barre oblique inverse suivie d'une n - que l'analyseur de JSON verra comme une séquence d'échappement et l'analyse syntaxique comme embedded nouvelle ligne. Comparez:

char const tl_json[] = "{\"a\":\"foo\\nbar\"}"; 

Ce code la chaîne JSON suivante, qui sera Parse:

{"a":"foo\nbar"} 

Par exemple, ce problème apparaît au moins une fois dans votre code:

... future of the Twitter API platform!\nhttps://blog ...