簡體   English   中英

將字符串轉換為JSON C#

[英]Turn string into json C#

我有正在使用的示例代碼。 json是http發布的結果。

var json = @"{'user': {
                        'country':'US',
                        'email':'testapi@example.com',
                        'first_name':'Test',
                        'last_name':'API',
                        'phone':null,
                        'zip':null,
                        'login_url':'https://new.site.com/xlogin/12325/abd9832cd92'
                        }
                    }";
        var jsonSerializer = new JavaScriptSerializer();
        var itemsList = (IDictionary<string, object>)jsonSerializer.DeserializeObject(json);
        var url = itemsList["user.login_url"];

itemsList["user.login_url"]我收到以下錯誤:

 The given key was not present in the dictionary.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary.

Source Error:


Line 545:        var jsonSerializer = new JavaScriptSerializer();
Line 546:        var itemsList = (IDictionary<string, object>)jsonSerializer.DeserializeObject(json);
Line 547:        var url = itemsList["user.login_url"];
Line 548:    }
Line 549:

我在這里做錯什么了嗎? 我應該如何從該對象訪問名字,姓氏和url等? 在此處輸入圖片說明

或者,我如何將此結果綁定到具有以下屬性的類? 我只需要一個指向好的資源的指針。

public class User
{
    public string Country { get; set; }
    public string Email { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Phone { get; set; }
    public string Zip { get; set; }
    public string LoginUrl { get; set; }
}

謝謝。

好吧,我真的不明白為什么您要使用IDictionary解析json對象。

  1. 使用Newtonsoft.Json代替jsonSerializer可以使用更多文章。
  2. 繼續http://json2csharp.com/並生成您的類以定義json(復制json並得到C#類)。

現在將您的json綁定到新的RootObject而不是用戶:

using System;
using Newtonsoft.Json;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var json = @"{'user': {
                        'country':'US',
                        'email':'testapi@example.com',
                        'first_name':'Test',
                        'last_name':'API',
                        'phone':null,
                        'zip':null,
                        'login_url':'https://new.site.com/xlogin/12325/abd9832cd92'
                        }
                    }";

            RootObject userObj = JsonConvert.DeserializeObject<RootObject>(json.ToString());

        }
    }


    //generated with http://json2csharp.com/
    public class User
    {
        public string country { get; set; }
        public string email { get; set; }
        public string first_name { get; set; }
        public string last_name { get; set; }
        public object phone { get; set; }
        public object zip { get; set; }
        public string login_url { get; set; }
    }

    public class RootObject
    {
        public User user { get; set; }
    }
}

“ user.login_url”是您希望在JavaScript中使用的屬性路徑...嘗試訪問字典鍵

var user = itemsList["user"] as IDictionary<string,object>;
var url = user["login_url"] as string;

itemsList [“ user”]包含第二個詞典。 因此,您可以使用以下命令導航到login_url變量

var user = (IDictionary<string, object>)itemsList["user"];
var login_url = user["login_url"];

嘗試使用http://json.net/ ,它將為您提供所需類型的字典。

暫無
暫無

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

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