簡體   English   中英

JsonConvert.SerializeObject 讀取結構,但不讀取值

[英]JsonConvert.SerializeObject reads the structure, but not the values

I tried this piece of source code (based on NewtonSoft's JSON NuGet library), for reading a JSON file into a JSON object:

string str_File_Content = File.ReadAllText(openFileDialog1.FileName);
Rootobject existing_root = JsonConvert.DeserializeObject<Rootobject>(str_File_Content);

...它幾乎起作用了:所有 JSON 對象都加載到existing_root中,如果是 arrays,對象的數量似乎是正確的。

但是:屬性的值似乎沒有填寫,如您在此處看到的:

JSON 文件摘錄:

{
    "project": {
        "common.DESCRIPTION": "Some information",

觀看 window 中existing_root根目錄摘錄:

Expected    :existing_root.project.commonDESCRIPTION : Some information
Real result :existing_root.project.commonDESCRIPTION : null

我該怎么做才能使JsonConvert.DeserializeObject()不僅處理結構,而且處理值?

您的 json 屬性名稱包含“。” 符號,對於 C# 屬性名稱無效,因此您可以使用JsonPropertyAttribute指定在(反)序列化期間使用的正確名稱:

public class Project
{
    [JsonProperty("common.DESCRIPTION")]
    public string commonDESCRIPTION { get; set; }
}

暫無
暫無

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

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