无法从 CouchDB 反序列化 Json

Cannot deserialize Json from CouchDB

我正在努力反序列化我得到的 Json。

看起来像这样:

{
    "seq": "13-g1AAAACLeJz",
    "id": "c32b670-37899h8c545",
    "changes": [
        {
            "rev": "9-68joc97b73df883a"
        }
    ]
}
{
    "seq": "15-g1AAAACLeJzLYWB-w",
    "id": "f73huvh3-of38j8fh",
    "changes": [
        {
            "rev": "4-10516b0f3"
        }
    ],
    "deleted": true
}
{
    "seq": "16-g1AAAACL4eJzL",
    "id": "M.Mustermann",
    "changes": [
        {
            "rev": "388-e6d350281"
        }
    ],
    "deleted": true
}
{
    "seq": "17-g1AAAACLr43_Q",
    "id": "f8h83hf-34f8h",
    "changes": [
        {
            "rev": "4-773re8f44e"
        }
    ],
    "deleted": true
}
{
    "seq": "18-g1AAwe_g",
    "id": "ewf/34r",
    "changes": [
        {
            "rev": "9-wehch87ewc"
        }
    ],
    "deleted": true
}
{
    "last_seq": "21-g1AA8wd998rAQ",
    "pending": 0
}

我的模特:

public class Root    {
        public List<Result> results { get; set; } 
        public string last_seq { get; set; } 
        public int pending { get; set; } 
    }    

    public class Result
    {
        public string seq { get; set; }
        public string id { get; set; }
        public List<Change> changes { get; set; }
        public bool? deleted { get; set; }
    }

    public class Change
    {
        public string rev { get; set; }
    }

我尝试了我找到的所有解决方案,但没有任何效果,我仍然遇到以下异常:

Newtonsoft.Json.JsonReaderException: 阅读完 JSON 内容后遇到的附加文本:{.路径 '',第 2 行,位置 0。 在 Newtonsoft.Json.JsonTextReader.Read()

我尝试了什么:

dynamic array = JsonConvert.DeserializeObject<Root>(json);

Root jsonObject = JsonConvert.DeserializeObject<Root>(json);

var jsonObject = JsonConvert.DeserializeObject<List<Root>>(json);

如果您可以要求更改您收到的 json,下面的 Json 应该有效。 如果你不能,我想你将不得不自己解析它。

"results" : [{
"seq": "13-g1AAAACLeJz",
"id": "c32b670-37899h8c545",
"changes": [
    {
        "rev": "9-68joc97b73df883a"
    }
]
},
{
    "seq": "15-g1AAAACLeJzLYWB-w",
    "id": "f73huvh3-of38j8fh",
    "changes": [
        {
            "rev": "4-10516b0f3"
        } 
    ],
    "deleted": true
},
{
    "seq": "16-g1AAAACL4eJzL",
    "id": "M.Mustermann",
    "changes": [
        {
            "rev": "388-e6d350281"
        }
    ],
    "deleted": true
},
{
    "seq": "17-g1AAAACLr43_Q",
    "id": "f8h83hf-34f8h",
    "changes": [
        {
            "rev": "4-773re8f44e"
        }
    ],
    "deleted": true
},
{
    "seq": "18-g1AAwe_g",
    "id": "ewf/34r",
    "changes": [
        {
            "rev": "9-wehch87ewc"
        }
    ],
    "deleted": true
}],
"last": {
    "last_seq": "21-g1AA8wd998rAQ",
    "pending": 0
}

您拥有的是一系列 JSON 文档,它们全部粘在一起。还让事情变得更尴尬的是,最后一个与其余的不同。所以你需要手动解析它们并做一些检查以查看你有什么样的对象。你可以利用JsonReader.SupportMultipleContent属性。例如,像这样:

//Somewhere to keep the parsed results
var results = new List<Result>();
Root root = null;

var serializer = new JsonSerializer();

using (var stringReader = new StringReader(Json))
using (var jsonReader = new JsonTextReader(stringReader))
{
    //Make sure the JsonReader knows we have multiple documents
    jsonReader.SupportMultipleContent = true;

    while (jsonReader.Read())
    {
        //Read in the next document
        var nextObject = JObject.ReadFrom(jsonReader);

        //Determine if we are on the last item or not
        if(nextObject["last_seq"] != null)
        {
            root = nextObject.ToObject<Root>();
        }
        else
        {
            results.Add(nextObject.ToObject<Result>());
        }
    }
}

//Store the results in the root object since this is how your classes have been structured
if(root != null)
{
    root.results = results;
}