久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

<small id='QdGqZ'></small><noframes id='QdGqZ'>

    <legend id='QdGqZ'><style id='QdGqZ'><dir id='QdGqZ'><q id='QdGqZ'></q></dir></style></legend>

    • <bdo id='QdGqZ'></bdo><ul id='QdGqZ'></ul>
  1. <i id='QdGqZ'><tr id='QdGqZ'><dt id='QdGqZ'><q id='QdGqZ'><span id='QdGqZ'><b id='QdGqZ'><form id='QdGqZ'><ins id='QdGqZ'></ins><ul id='QdGqZ'></ul><sub id='QdGqZ'></sub></form><legend id='QdGqZ'></legend><bdo id='QdGqZ'><pre id='QdGqZ'><center id='QdGqZ'></center></pre></bdo></b><th id='QdGqZ'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='QdGqZ'><tfoot id='QdGqZ'></tfoot><dl id='QdGqZ'><fieldset id='QdGqZ'></fieldset></dl></div>
    1. <tfoot id='QdGqZ'></tfoot>

      將 JSON 反序列化為 C# 類

      Deserialize JSON to C# Classes(將 JSON 反序列化為 C# 類)

            <bdo id='epxhA'></bdo><ul id='epxhA'></ul>
                <tbody id='epxhA'></tbody>
            • <tfoot id='epxhA'></tfoot>

              <small id='epxhA'></small><noframes id='epxhA'>

              1. <legend id='epxhA'><style id='epxhA'><dir id='epxhA'><q id='epxhA'></q></dir></style></legend>
                <i id='epxhA'><tr id='epxhA'><dt id='epxhA'><q id='epxhA'><span id='epxhA'><b id='epxhA'><form id='epxhA'><ins id='epxhA'></ins><ul id='epxhA'></ul><sub id='epxhA'></sub></form><legend id='epxhA'></legend><bdo id='epxhA'><pre id='epxhA'><center id='epxhA'></center></pre></bdo></b><th id='epxhA'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='epxhA'><tfoot id='epxhA'></tfoot><dl id='epxhA'><fieldset id='epxhA'></fieldset></dl></div>
                本文介紹了將 JSON 反序列化為 C# 類的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                問題描述

                以下是我在成功創(chuàng)建新的工作代碼"條目后從 REST API 獲得的(略微)精簡的響應(yīng).我需要將響應(yīng)反序列化為某些類,但我很難過.

                Below is a (slightly) stripped down response I get from a REST API upon successful creation of a new "job code" entry. I need to deserialize the response into some classes, but I'm stumped.

                作為參考,我在 .NET 3.5 中使用 JSON.NET(在 SQL Server 2008 R2 的 SSIS 腳本中運行)來嘗試反序列化.這是 JSON - 我顯然無法控制它,因為它來自其他人的 API:

                For reference, I'm using JSON.NET in .NET 3.5 (running in a SSIS script in SQL Server 2008 R2) to attempt my deserialization. Here's the JSON - which I obviously have no control over as it's coming from someone else's API:

                {
                   "results":{
                      "jobcodes":{
                         "1":{
                            "_status_code":200,
                            "_status_message":"Created",
                            "id":444444444,
                            "assigned_to_all":false,
                            "billable":true,
                            "active":true,
                            "type":"regular",
                            "name":"1234 Main Street - Jackson"
                         },
                         "2":{
                            "_status_code":200,
                            "_status_message":"Created",
                            "id":1234567890,
                            "assigned_to_all":false,
                            "billable":true,
                            "active":true,
                            "type":"regular",
                            "name":"4321 Some Other Street - Jackson"
                         }
                      }
                   }
                }
                

                在我的 C# 代碼中,我確實定義了一個JobCode"類,它僅將 JSON 值部分映射到屬性 - 我對返回給我的所有數(shù)據(jù)不感興趣:

                In my C# code, I do have a "JobCode" class defined which only partially maps the JSON values to properties - I'm not interested in all of the data that's returned to me:

                [JsonObject]
                class JobCode
                {
                    [JsonProperty("_status_code")]
                    public string StatusCode { get; set; }
                    [JsonProperty("_status_message")]
                    public string StatusMessage { get; set; }
                    [JsonProperty("id")]
                    public string Id {get; set;}
                    [JsonProperty("name")]
                    public string Name { get; set; }
                
                    //-------------------------------------------------------------------------------
                    // Empty constructor for JSON serialization support
                    //-------------------------------------------------------------------------------
                
                    public JobCode() { }
                }
                

                我正在嘗試通過此調(diào)用反序列化數(shù)據(jù):

                I'm attempting to deserialize the data via this call:

                newResource = JsonConvert.DeserializeObject<JobCode>(jsonResponse);
                

                其中 jsonResponse 是上面輸出的代碼.
                當我執(zhí)行代碼時,newResource"總是返回為空 - 這并不意外,因為我知道數(shù)據(jù)中實際上有多個作業(yè)代碼,并且此代碼試圖將其反序列化為單個 JobCode 對象.我嘗試創(chuàng)建一個名為JobCodes"的新類,如下所示:

                Where jsonResponse is the code outputted above.
                When I execute the code, "newResource" always comes back as null - which is not unexpected because I know that there are actually multiple jobcodes in the data and this code is trying to deserialize it into a single JobCode object. I tried creating a new class called "JobCodes" that looks like this:

                class JobCodes
                {
                    [JsonProperty("jobcodes")]
                    public List<JobCode>_JobCodes { get; set; }
                }
                

                然后我嘗試調(diào)用這個:

                newResource = JsonConvert.DeserializeObject<JobCodes>(jsonResponse);
                

                但問題仍然存在 - 我的返回對象為空.我認為讓我失望的是1"和2"標識符的存在.我不知道如何在我的對象設(shè)計和/或 JSON.NET 類/屬性屬性(如 [JsonObject]、[JsonProperty] 等)的使用中說明它們的存在.

                But the issue persists - my return object is null. What's throwing me off, I think, is the presence of the "1" and "2" identifiers. I don't know how to account for their presence in my object design and/or usage of the JSON.NET class / property attributes like [JsonObject],[JsonProperty], etc.

                當我通過 JSON2CSharp 運行 JSON 數(shù)據(jù)時,它會構(gòu)造一些看起來很奇怪的類,因此這并沒有被證明太有效.我已經(jīng)用幾個不同的驗證器驗證了 JSON 并且一切都檢查出來了——我只是不知道我在這里缺少什么.

                When I run the JSON data through JSON2CSharp, it constructs some weird-looking classes, so that hasn't proven too effective. I've validated the JSON with several different validators and it all checks out - I just don't know what I'm missing here.

                最終,我想從 JSON 數(shù)據(jù)中返回一個 List,但我不知道我需要做什么才能實現(xiàn)這一目標.

                Ultimately, I'd like to return a List from the JSON data, but I'm stumped on what I need to do to make that happen.

                推薦答案

                你的問題是雙重的:

                1. 您沒有在根級別定義類.類結(jié)構(gòu)需要匹配整個 JSON,不能只從中間反序列化.
                2. 只要你有一個可以改變鍵的對象,你就需要使用Dictionary.普通課程對此不起作用;List<T> 也不會.
                1. You don't have a class defined at the root level. The class structure needs to match the entire JSON, you can't just deserialize from the middle.
                2. Whenever you have an object whose keys can change, you need to use a Dictionary<string, T>. A regular class won't work for that; neither will a List<T>.

                讓你的課程像這樣:

                class RootObject
                {
                    [JsonProperty("results")]
                    public Results Results { get; set; }
                }
                
                class Results
                {
                    [JsonProperty("jobcodes")]
                    public Dictionary<string, JobCode> JobCodes { get; set; }
                }
                
                class JobCode
                {
                    [JsonProperty("_status_code")]
                    public string StatusCode { get; set; }
                    [JsonProperty("_status_message")]
                    public string StatusMessage { get; set; }
                    [JsonProperty("id")]
                    public string Id { get; set; }
                    [JsonProperty("name")]
                    public string Name { get; set; }
                }
                

                然后,像這樣反序列化:

                Then, deserialize like this:

                RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
                

                這里的工作演示

                這篇關(guān)于將 JSON 反序列化為 C# 類的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

                相關(guān)文檔推薦

                Ignore whitespace while reading XML(讀取 XML 時忽略空格)
                XML to LINQ with Checking Null Elements(帶有檢查空元素的 XML 到 LINQ)
                Reading XML with unclosed tags in C#(在 C# 中讀取帶有未閉合標簽的 XML)
                Parsing tables, cells with Html agility in C#(在 C# 中使用 Html 敏捷性解析表格、單元格)
                delete element from xml using LINQ(使用 LINQ 從 xml 中刪除元素)
                Parse malformed XML(解析格式錯誤的 XML)

                  <tbody id='x2BnG'></tbody>

                  • <small id='x2BnG'></small><noframes id='x2BnG'>

                      <bdo id='x2BnG'></bdo><ul id='x2BnG'></ul>
                    • <legend id='x2BnG'><style id='x2BnG'><dir id='x2BnG'><q id='x2BnG'></q></dir></style></legend>
                    • <i id='x2BnG'><tr id='x2BnG'><dt id='x2BnG'><q id='x2BnG'><span id='x2BnG'><b id='x2BnG'><form id='x2BnG'><ins id='x2BnG'></ins><ul id='x2BnG'></ul><sub id='x2BnG'></sub></form><legend id='x2BnG'></legend><bdo id='x2BnG'><pre id='x2BnG'><center id='x2BnG'></center></pre></bdo></b><th id='x2BnG'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='x2BnG'><tfoot id='x2BnG'></tfoot><dl id='x2BnG'><fieldset id='x2BnG'></fieldset></dl></div>

                          <tfoot id='x2BnG'></tfoot>
                          主站蜘蛛池模板: 国产欧美一区二区三区另类精品 | 精品区 | 国产精品国产精品国产专区不卡 | 一级做a爰片性色毛片16 | 欧美成人精品一区二区男人看 | 亚洲成人一区二区在线 | 中文字幕a√ | 欧美一区二区三区四区五区无卡码 | 日韩精品视频中文字幕 | 在线日韩| 欧美精品一区二区免费 | 99re在线视频| 亚洲精品一区在线观看 | 国产一区www| 亚洲欧美另类在线 | 天天射色综合 | 在线观看黄免费 | 毛片免费视频 | 91av亚洲| 亚洲欧美网站 | 亚洲国产免费 | 91在线免费视频 | 国产精品久久久久久 | www.成人.com| a级免费视频| 国产一级免费视频 | 蜜桃一区二区三区 | 亚洲综合久久精品 | 国产精品美女久久久久aⅴ国产馆 | 精精精精xxxx免费视频 | 狠狠狠干 | 国产精品久久久久久久7电影 | 青青草一区 | 中文字幕一区二区三 | 欧美寡妇偷汉性猛交 | 日韩一级二级片 | 国产精品视频一 | 做a视频在线观看 | 久久久91| 国产不卡视频 | 久久久高清 |