簡體   English   中英

反序列化JSON對象.net

[英]deserializing JSON object .net

我在反序列化json對象時遇到問題。 我在stackoverflow上閱讀了很多線程,但是沒有找到解決方案。 我使用.net 2.0和Newtonsoft庫。

以下是json字符串:

{
    "data": {
        "custom_fields": [{
            "field": "segmentid",
            "value": "B"
        },
        {
            "field": "subsegmentid",
            "value": "TM3"
        },
        {
            "field": "contactpersonid",
            "value": "000187_003"
        },
        {
            "field": "firstname",
            "value": "ZBIGNIEW"
        },
        {
            "field": "agreetment",
            "value": "1"
        },
        {
            "field": "contactname",
            "value": "ZBIGNIEW TESTOWY"
        },
        {
            "field": "decisionmaking",
            "value": "0"
        },
        {
            "field": "lastpurchase",
            "value": ""
        },
        {
            "field": "agethresholds",
            "value": "0"
        },
        {
            "field": "tendercust",
            "value": ""
        }],
        "email": "myself@example.com",
        "state": "1"
    },
    "status": "OK"
}

我開發了兩個類,但仍然出現錯誤。

無法將當前JSON對象(例如{\\“ name \\”:\\“ value \\”})反序列化為類型'System.Collections.Generic.List`1 [FMIntegration.DataFM]',因為該類型需要JSON數組(例如[ \\ 1,2,3])以正確地反序列化。\\ r \\ n要解決此錯誤,請將JSON更改為JSON數組(例如[1,2,3])或更改反序列化的類型,使其成為常規的.NET類型。 (例如,不是整數之類的原始類型,不是數組或List之類的集合類型),可以從JSON對象反序列化。 也可以將JsonObjectAttribute添加到類型中,以強制其從JSON對象反序列化。\\ r \\ nPath'data.email'

var SubscriberGet = JsonConvert.DeserializeObject<SubscriberGet>(json); 

public class SubscriberGet
{
    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("data")]
    public List<DataFM> Data { get; set; }
}

public class DataFM
{
    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("custom_fields")]
    public List<String> custom_fields { get; set; }

    [JsonProperty("state")]
    public string State { get; set; }
}

自定義字段不是字符串列表,而是“字段和值”對的列表。

嘗試這個:

public class SubscriberGet
{
    [JsonProperty("status")]
    public string Status { get; set; }

    [JsonProperty("data")]
    public List<DataFM> Data { get; set; }
}

public class DataFM
{
    [JsonProperty("email")]
    public string Email { get; set; }

    [JsonProperty("custom_fields")]
    public List<CustomField> custom_fields { get; set; }

    [JsonProperty("state")]
    public string State { get; set; }
}
public class CustomField
{
    [JsonProperty("field")]
    public string field { get; set; }
    [JsonProperty("value")]
    public string value { get; set; }
}

這是處理我的json字符串的正確對象(感謝LB的鏈接)

public class CustomField
{
    public string field { get; set; }
    public string value { get; set; }
}

public class Data
{
    public List<CustomField> custom_fields { get; set; }
    public string email { get; set; }
    public string state { get; set; }
}

public class RootObject
{
    public Data data { get; set; }
    public string status { get; set; }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM