如何从C(http响应)中的sting中提取特定字符

如何从C(http响应)中的sting中提取特定字符,c,json,request,C,Json,Request,我想从http响应中提取Json对象。但是我怎样才能提取它呢 这是我的字符串: GET/axis cgi/dynamiccoverlay.cgi?action=settext&text=HTTP/1.1200 OK#015#012服务器:nginx#015#012日期:2015年9月18日星期五09:39:01 GMT#015#012内容类型:application/json;5月5日,5月5日,5月5日,5月5日,5月5日,5月5日,5月5日,5月5日,12月5日,5月5日,012X来源:5月

我想从http响应中提取Json对象。但是我怎样才能提取它呢

这是我的字符串:

GET/axis cgi/dynamiccoverlay.cgi?action=settext&text=HTTP/1.1200 OK#015#012服务器:nginx#015#012日期:2015年9月18日星期五09:39:01 GMT#015#012内容类型:application/json;5月5日,5月5日,5月5日,5月5日,5月5日,5月5日,5月5日,5月5日,12月5日,5月5日,012X来源:5月5日,5月5日,5月5日,5月5日,5日,5月5日,5日,5日,5月5日,5日,5日,5日,5月5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5日,5来源:来源:来源:来源:来源:来源:来源:来源:来源:来源:来源:来源:日,5日,5日,5日,5日,5日,5日,5日145.77,“纬度”:-16.92},“天气”:[{“id”:800,“主要”:“晴朗”,“说明”:“天空晴朗”、“图标”:“01n”}]、“基地”:“车站”、“主要”:{“温度”:296.49,“压力”:1013,“湿度”:50,“温度最低”:295.93,“温度最高”:297.15},“能见度”:10000,“风”:{“速度”:2.1,“度”:100},“云”:{“所有”:0},“dt”:1442566613,“系统”:{“类型”:1,“身份证”:8166,“信息”:0.0075,“国家”:“AU”,“日出”:1442520614,“日落”:1442563944},“身份证”:21797,“凯恩斯”:“姓名”:“凯恩斯”:,“cod:200}012#015#0120#015#012#015#012 HTTP/1.0#015#012授权:基本cm9vdDpzdHJlYW0=#015#012主机:192.168.2.3


在你开始一遍又一遍地问同样的问题之前,这里有一个解决方案:

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/*
 *      Extracts the fist JSON object between balanced curly braces
 *      and copies it to the given buffer. Returns the length of the
 *      extracted string. At most nbuf -1 characters will be copied 
 *      and the result is always null-terminated. A return value
 *      of nbuf or higher indicates that the output aws truncated.
 *      If no object is found, -1 is returned.
 */
int get_json(char buf[], size_t nbuf, const char *str)
{
    const char *p, *q;
    int braces = 0;

    p = strchr(str, '{');
    if (p == NULL) return -1;

    for (q = p; *q; q++) {
        if (*q == '{') braces++;
        if (*q == '}') {
            braces--;

            if (braces == 0) {
                int len = q - p + 1;

                return snprintf(buf, nbuf, "%.*s", len, p);
            }
        }
    }

    return -1;      // brace mismatch;
}

int main()
{
    char *str = "GET /axis-cgi/dynamicoverlay.cgi?action=settext&"
        "text=HTTP/1.1 200 OK#015#012Server: nginx#015#01"
        "2Date: Fri, 18 Sep 2015 09:39:01 GMT#015#012Cont"
        "ent-Type: application/json; charset=utf-8#015#01"
        "2Transfer-Encoding: chunked#015#012Connection: k"
        "eep-alive#015#012X-Source: redis#015#012Access-C"
        "ontrol-Allow-Origin: *#015#012Access-Control-All"
        "ow-Credentials: true#015#012Access-Control-Allow"
        "-Methods: GET, POST#015#012#015#0121c1#015#012{\""
        "coord\":{\"lon\":145.77,\"lat\":-16.92},\"weather\":[{\""
        "id\":800,\"main\":\"Clear\",\"description\":\"Sky is Cle"
        "ar\",\"icon\":\"01n\"}],\"base\":\"stations\",\"main\":{\"te"
        "mp\":296.49,\"pressure\":1013,\"humidity\":50,\"temp_m"
        "in\":295.93,\"temp_max\":297.15},\"visibility\":10000"
        ",\"wind\":{\"speed\":2.1,\"deg\":100},\"clouds\":{\"all\":"
        "0},\"dt\":1442566613,\"sys\":{\"type\":1,\"id\":8166,\"me"
        "ssage\":0.0075,\"country\":\"AU\",\"sunrise\":144252061"
        "4,\"sunset\":1442563944},\"id\":2172797,\"name\":\"Cair"
        "ns\",\"cod\":200}#012#015#0120#015#012#015#012 HTTP"
        "/1.0#015#012Authorization: Basic cm9vdDpzdHJlYW0"
        "=#015#012Host: 192.168.2.3";
    char buf[512];
    int n;

    n = get_json(buf, sizeof(buf), str);
    if (n >= 0) puts(buf);

    return 0;
}
#包括
#包括
#包括
/*
*提取平衡大括号之间的第一个JSON对象
*并将其复制到给定的缓冲区。返回数据的长度
*提取字符串。最多复制nbuf-1个字符
*结果总是以null结尾。返回值
*nbuf或更高值表示输出被截断。
*如果未找到对象,则返回-1。
*/
int get_json(char buf[],size_t nbuf,const char*str)
{
常量字符*p,*q;
int大括号=0;
p=strchr(str,{');
if(p==NULL)返回-1;
对于(q=p;*q;q++){
if(*q=='{')大括号++;
如果(*q=='}'){
大括号--;
如果(大括号==0){
int len=q-p+1;
返回snprintf(buf,nbuf,'%s',len,p);
}
}
}
return-1;//大括号不匹配;
}
int main()
{
char*str=“GET/axis cgi/dynamicoverlay.cgi?action=settext&”
“text=HTTP/1.1200正常#015#012服务器:nginx#015#01”
“2日期:2015年9月18日星期五09:39:01格林威治标准时间015年12月12日续”
“ent类型:application/json;charset=utf-8#015#01”
“2传输编码:分块#015#012连接:k”
“eep活动#015#012X来源:redis#015#012Access-C”
“控制允许原点:*#015#012访问控制全部”
“ow凭据:true#015#012访问控制允许”
“-方法:获取、发布#015#012#015#0121c1#015#012{”
“coord\”:{“lon\”:145.77,“lat\”:-16.92},“weather\”:[{”
“id\”:800、\“main\:\“Clear\”、\“description\”:\“Sky is Cle”
“ar\”、“icon\”:“01n\”}]、“base\”:“stations\”、“main\”:{“te”
“mp\”:296.49、“压力\”:1013、“湿度\”:50、“温度”
“在\':295.93、\'temp\u max\':297.15}、\'visibility\':10000中”
“,”风“:{”速度“:2.1,\”度“:100},\”云“:{”所有“:”
“0},\'dt\':1442566613,\'sys\':{\'type\':1,\'id\':8166,\'me”
“消息\”:0.0075,“国家\”:“非洲\”,“日出\”:144252061”
“4,\'sunset\':1442563944},\'id\':2172797,\'name\':\'Cair”
“ns\”,“cod\”:200}012#015#0120#015#012#015#012 HTTP”
“/1.0#015#012授权:基本cm9vdDpzdHJlYW0”
“=#015#012主机:192.168.2.3”;
char-buf[512];
int n;
n=get_json(buf,sizeof(buf),str);
如果(n>=0)输入(buf);
返回0;
}

谢谢!这正是我需要的!