JSON使用JsonConvert进行字典

时间:2019-05-27 09:49:29

标签: c#

我正在从JSON格式的API调用访问数据。我正在尝试将其放入字典中,以便可以访问数据。这对于简单的JSON对象非常有效,但对于嵌套对象而言,“在解析值时遇到意外字符:[。路径'cluster_functions',第1行,位置539”,则失败。

我的JSON:

@Entity
@Table(name = "fact_gdpr_data")
@SQLInsert(sql = "INSERT INTO fact_gdpr_data (LocalTimezoneAgreementAcceptCount, LocalTimezoneAgreementNoticeViewCount, UtcAgreementAcceptCount, " +
        "UtcAgreementNoticeViewCount, TenantId, DeviceId, DateId, AppId) VALUES (?, ?, ?, ?, ?, ?, ?, ?) " +
        "ON DUPLICATE KEY UPDATE " +
        "LocalTimezoneAgreementAcceptCount = VALUES(LocalTimezoneAgreementAcceptCount) + LocalTimezoneAgreementAcceptCount, " +
        "LocalTimezoneAgreementNoticeViewCount = VALUES(LocalTimezoneAgreementNoticeViewCount) + LocalTimezoneAgreementNoticeViewCount, " +
        "UtcAgreementAcceptCount = VALUES(UtcAgreementAcceptCount) + UtcAgreementAcceptCount, " +
        "UtcAgreementNoticeViewCount = VALUES(UtcAgreementNoticeViewCount) + UtcAgreementNoticeViewCount", check = ResultCheckStyle.COUNT)
public class FactGdprData implements Serializable {
    @Column(name = "UtcAgreementAcceptCount")
    private int utcAgreementAcceptCount;

    @Column(name = "LocalTimezoneAgreementAcceptCount")
    private int localTimezoneAgreementAcceptCount;

    @Column(name = "UtcAgreementNoticeViewCount")
    private int utcAgreementNoticeViewCount;

    @Column(name = "LocalTimezoneAgreementNoticeViewCount")
    private int localTimezoneAgreementNoticeViewCount;

    @Id
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "TenantId", referencedColumnName = "Id")
    private DimTenant tenantId;

    @Id
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "AppId", referencedColumnName = "Id")
    private DimApp appId;

    @Id
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DeviceId", referencedColumnName = "Id")
    private DimDevice deviceId;

    @Id
    @OneToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "DateId", referencedColumnName = "Id")
    private DimDate dateId;

    // Getters, Setters etc.
}

代码:

{
"id": "xxxxx",
"uuid": "xxxxx",
"cluster_incarnation_id": 151013752,
"cluster_functions": ["NDFS"],
"is_lts": true,
"num_nodes": 4,
"block_serials": ["17xxx"],
"ntp_servers": ["172.26.140.50", "ntp.xxx.be"],
"service_centers": [],
"http_proxies": [],
"rackable_units": [{
    "id": 23,
    "rackable_unit_uuid": "09f211d1-8fb4-xxxx-86e2-ce819xxxx29",
    "positions": ["1", "2", "3", "4"],
    "nodes": [6, 7, 8, 9]
}],
"public_keys": [],
"smtp_server": null,
"hypervisor_types": ["kKvm"],
"cluster_redundancy_state": {
    "current_redundancy_factor": 2,
    "desired_redundancy_factor": 2,
    "redundancy_status": {
        "kCassandraPrepareDone": true,
        "kZookeeperPrepareDone": true
    }
}
}

我不确定如何继续。我应该逐行解析,如果值不是字符串,是否还要进行反序列化?但是,如果我不知道JSON的格式(以及级别的数量),该怎么做。

还是有更有效的方法来解析字典,无论层数如何?

谢谢

2 个答案:

答案 0 :(得分:2)

尝试:

using (StreamReader reader = new StreamReader(HttpResponseStream))
{
    //Response.Code = 1;
    string body = reader.ReadToEnd();
    consoleoutput("REST: result" + body);

    resultdict = JsonConvert.DeserializeObject<Dictionary<string, dynamic>>(body);
}

无论如何,你不应该那样做。

您应该制作一个描述每种可能的属性的类,并像这样进行工作:

public class Entity { /* properties definitions... */ }

然后

using (StreamReader reader = new StreamReader(HttpResponseStream))
{
    //Response.Code = 1;
    string body = reader.ReadToEnd();
    consoleoutput("REST: result" + body);

    List<Entity> entities = JsonConvert.DeserializeObject<Entity>(body).ToList();
}

答案 1 :(得分:0)

您可以按如下所示反序列化JSON

var result =JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(body);