如何从这个json结构中获取数据

时间:2016-01-09 05:03:13

标签: json

JSON Viewer不支持此结构。我们可以从这个结构中获取数据吗?

{
"Birthday": "[{"Name":"Gyanendra Bajpai (11198)","Date":"08 Jan 2016","EventCategory":"Birthday","EmployeeID":"145"}]"
}

3 个答案:

答案 0 :(得分:1)

static struct mosquitto *m = NULL;

int main(){

    mosquitto_lib_init();
    printf("LIBMOSQUITTO %d\n", LIBMOSQUITTO_VERSION_NUMBER);

    if ((m = mosquitto_new("rtr", 1, NULL)) == NULL) {
        fprintf(stderr, "Out of memory.\n");
        exit(1);
    }

    int rc = mosquitto_tls_set(m,
            "/home/ca.crt",     /* cafile */
            NULL,           /* capath */
            "/home/client.crt",     /* certfile */
            "/home/client.key",     /* keyfile */
            NULL            /* pw_callback() */
            );

    if (rc != MOSQ_ERR_SUCCESS) {
        fprintf(stderr, "Cannot set TLS CA: %s (check path names)\n",
                mosquitto_strerror(rc));
        exit(3);
    }
#if 1
    mosquitto_tls_opts_set(m,
            SSL_VERIFY_PEER,
            NULL,           /* tls_version: "tlsv1.2", "tlsv1" */
            NULL            /* ciphers */
            );
    mosquitto_tls_insecure_set(m, 1);
#endif
    if ((rc = mosquitto_connect(m, "localhost", 8884, 20)) != MOSQ_ERR_SUCCESS) {
        fprintf(stderr, "%d: Unable to connect: %s\n", rc,
                mosquitto_strerror(rc));
        perror("");
        exit(2);
    }

    //mosquitto_loop_forever(m, -1, 1);
    mosquitto_destroy(m);
    mosquitto_lib_cleanup();
}

答案 1 :(得分:1)

您的JSON无效,您可以使用JSONLint等在线网站进行验证。

我建议您将其更改为以下内容:

{
    "Birthday": {
        "Name": "Gyanendra Bajpai(11198)",
        "Date": "08 Jan 2016 ",
        "EventCategory": "Birthday",
        "EmployeeID": "145"
    }
}

然后,您可以按以下方式解析它:

try {
    JSONObject jsonObject = new JSONObject(jsonString);
    if (!jsonObject.isNull("Birthday")) {
        JSONObject jsonObject1 = jsonObject.getJSONObject("Birthday");
        if (jsonObject1 != null) {
            String name = jsonObject1.optString("Name");
            String date = jsonObject1.optString("Date");
            String eventCategory = jsonObject1.optString("EventCategory");
            String employeeID = jsonObject1.optString("EmployeeID");
        }
    }
} catch (JSONException e) {
    e.printStackTrace();
}

答案 2 :(得分:0)

问题是您错误地嵌套了引号。如果没有正确的转义,您无法在另一个"内使用"。因此,要么从"左右删除[],要么正确转义所有引号。