簡體   English   中英

如何使用JSon.net C#序列化雙Json數組

[英]How to serialize double Json Array with JSon.net C#

我有一些Json看起來像這樣:

{
    "result":
    [
        [
            {
                "CODE_DOC":"1",
                "NOM_DOC":"doc1.pdf",
                "CODE_CAT":"1",
                "VERSIONS":
                [
                    {
                    "CODE_VRS":"1",
                    "CODE_FIC":"1",
                    "NOM_VRS":"1",
                    "NOM_FIC":"doc1.pdf",
                    "LOG_PERS":"zzzz",
                    "DATE_ENT":"1492179997"
                    }
                ]
            },
            {
                "CODE_DOC":"2",
                "NOM_DOC":"doc2.pdf",
                "CODE_CAT":"2",
                "VERSIONS":
                [
                    {
                        "CODE_VRS":"2",
                        "CODE_FIC":"5",
                        "NOM_VRS":"45",
                        "NOM_FIC":"doc2.pdf",
                        "LOG_PERS":"jajaja",
                        "DATE_ENT":"1492161426"
                    }
                ]
            }
        ]
    ]
}

我想得到一個對象列表,就像:

public class Document
{
    int codeDoc;
    string nomDoc;
    int codeCat;
    Dictionary<String, String> versions;
}

但是問題是我一開始不知道如何處理該雙精度數組。 有誰知道我該怎么做? 謝謝你,祝你有美好的一天

您可以使用

var obj = JObject.Parse(json);

然后自己遍歷內部數組。 或者,您可以創建一個類

class Foo {
    public List<List<Document>> result { get; set; }
}

然后,您將通過訪問result [0]來訪問內部數組(假設外部數組的長度始終為1)。

要解決有關如何在開始時傳遞雙精度數組的問題,請向JToken添加零索引。 這是我的示例,首先讓我們創建對象類:

using Newtonsoft.Json.Linq;

public class JsonDoc
{
    //I am adding an index on constructor for looping purposes
    public JsonDoc(string json, int index)
    {
        JObject jObject = JObject.Parse(json);

        //Add an index of zero so that it will read the result node
        JToken jResult = jObject["result"][0];

        codedoc = (string)jResult[index]["CODE_DOC"].Value<string>();
        nomdoc = (string)jResult[index]["NOM_DOC"].Value<string>();
        codecat = (string)jResult[index]["CODE_CAT"].Value<string>();
        versions = jResult[index]["VERSIONS"].ToArray();
    }

    //This constructor will get the length of the array under result node
    public JsonDoc(string json)
    {
        JObject jObject = JObject.Parse(json);
        JToken jResult = jObject["result"][0];
        intCount = jResult.Count();
    }

    public string codedoc { get; set; }
    public string nomdoc { get; set; }
    public string codecat { get; set; }
    public Array versions { get; set; }
    public int intCount { get; set; }
}

然后我們讀取Json數據,在這種情況下,我將其放在文件中,然后使用StreamReader讀取它。 確切的數據與您的示例相同。 然后將其循環兩次,因為您的數據包含兩個對象。

using (StreamReader sr = new StreamReader(@"C:\Folder\json.json"))
        {
            string json = sr.ReadToEnd();
            JsonDoc jSon = new JsonDoc(json);

            //This will get the length of the array under result node
            int intCount = jSon.intCount;
            List<JsonDoc> lstResult = new List<JsonDoc>();
            for (int x = 0; x < intCount; x++)
            {
                lstResult.Add(new JsonDoc(json, x));
            }
        }

結果數據將是兩個具有版本數組的對象的列表:

在此處輸入圖片說明

暫無
暫無

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

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