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

时间:2015-09-18 09:51:35

标签: c json request

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

这是我的字符串:

  

GET /axis-cgi/dynamicoverlay.cgi?action=settext&text=HTTP/1.1 200 OK#015#012Server:nginx#015#012日期:2015年9月18日星期五09:39:01 GMT#015#012Content -Type:application / json; charset = utf-8#015#012Transfer-Encoding:chunked#015#012Connection:keep-alive#015#012X-Source:redis#015#012Access-Control-Allow-Origin:*#015#012Access-Control-Allow-证书:true#015#012Access-Control-Allow-Methods:GET,POST#015#012#015#0121c1#015#012 {" coord":{" lon":145.77 " LAT": - 16.92}"天气":[{" ID" 800"主":"清除& #34;,"说明":"天空晴朗","图标":" 01n"}],"基地&# 34;:"站""主" {"温度":296.49"压力":1013,"湿度&# 34; 50" temp_min":295.93" temp_max":297.15},"能见度":10000"风":{& #34;速度":2.1,"度":100}"云" {"所有":0}," DT&# 34;:1442566613," SYS" {"类型":1," ID":8166,"消息":0.0075,&# 34;国家":" AU""日出":1442520614,"日落":1442563944}" ID":2172797, "名称":"凯恩斯""鳕鱼":200}#012#015#0120#015#012#015#012 HTTP / 1.0#015#012授权:基本cm9vdDpzdHJlYW0 =#015#012主机:192.168.2.3

1 个答案:

答案 0 :(得分:1)

在你开始反复提出同样的问题之前,这是一个解决方案:

#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;
}