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

How to extract specific characters from a sting in C (http response)

我想从我的 http 响应中提取 Json 对象。但是如何提取呢?

这是我的字符串:

GET /axis-cgi/dynamicoverlay.cgi?action=settext&text=HTTP/1.1 200 OK#015#012Server: nginx#015#012Date: Fri, 18 Sep 2015 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-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 Clear","icon":"01n"}],"base":"stations","main":{"temp":296.49,"pressure":1013,"humidity":50,"temp_min":295.93,"temp_max":297.15},"visibility":10000,"wind":{"speed":2.1,"deg":100},"clouds":{"all":0},"dt":1442566613,"sys":{"type":1,"id":8166,"message":0.0075,"country":"AU","sunrise":1442520614,"sunset":1442563944},"id":2172797,"name":"Cairns","cod":200}#012#015#0120#015#012#015#012 HTTP/1.0#015#012Authorization: Basic cm9vdDpzdHJlYW0=#015#012Host: 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;
}