According to Baidu's speech recognition example, show how C uses Cjson

Source: Internet
Author: User

The previous article shows how to call C + + method According to Baidu speech recognition example, This article is based on Baidu speech recognition, show how to use Cjson,cjson is a C write JSON parser, very useful, you can use it to generate a json, It can also be used to parse the JSON Value.

In the code I wrote about getting tokens through skey.

Char *token = (char *) malloc (max_buffer_size);

Char host[max_buffer_size];

snprintf (host, sizeof (host),

"https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=%s&client_secret=%s ",

apiKey, secretkey);

FILE *FPP = NULL;

Char cmd[max_buffer_size];

Char *result = (char *) malloc (max_result_size);

Char *curl_cmd = "curl-s";

Char *yinhao = "\" ";

strcpy (cmd, curl_cmd);

strcat (cmd, yinhao);

strcat (cmd, host);

strcat (cmd, yinhao);

FPP = Popen (cmd, "r");

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

Fgets (result, max_result_size, fpp);

Pclose (fpp);

If (result! = NULL)

{

Cjson *root;

root = Cjson_parse (result);

If (!root)

{

printf ("\nget Root Failed");

return-1;

}

Token1 = Cjson_getobjectitem (root, "access_token")->valuestring;

strcpy (token, token1);

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

Cjson_delete (root);

Free (result);

}

Result is the JSON result, how to get the value of each node after getting json? You need to use the IF (result!=null) code, first Cjson define a root node Cjson *root, and then through the cjson_parse to obtain the root node, the root node, then through the Cjson_getobjectitem ( root, "access_token") Gets the value of the Access_token at the level below the root, and its JSON structure is like this

{

access_tokeen:xxxxxx,

Len:xxxxxxx

}

, after use to Cjson_delete.

Baidu Speech recognition post is submitted by json, so you need to assemble a json, the assembly code is as Follows:

Cjson *buffer;

Buffer = Cjson_createobject ();

Cjson_additemtoobject (buffer, "format", cjson_createstring ("pcm"));

Cjson_additemtoobject (buffer, "rate", cjson_createstring ("8000"));

Cjson_additemtoobject (buffer, "channel", cjson_createstring ("1"));

Cjson_additemtoobject (buffer, "token", cjson_createstring (token));

Cjson_additemtoobject (buffer, "cuid", cjson_createstring (cuid));

Cjson_additemtoobject (buffer, "len", cjson_createstring (content_length));

Cjson_additemtoobject (buffer, "speech", cjson_createstring (decode_data));

A cjson_createobject to bump into an object and then add a node to the object via cjson_additemtoobject, cjson_additemtoobject (buffer, "format", cjson_ Createstring ("pcm")); Format is the node name, and PCM is the value of the Node.

The resulting effect is

According to Baidu's speech recognition example, show how C uses Cjson

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.