把字符串变成json C#

时间:2014-10-31 14:22:25

标签: c# json

我有这个与我合作的示例代码。 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:

我在这里做错了吗?我该如何从这个对象访问名字,姓氏和网址等? enter image description here

或者,如何将此结果绑定到具有以下属性的类?我只需要一个指向好资源的指针。

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; }
}

感谢。

4 个答案:

答案 0 :(得分:2)

我真的不明白你为什么用IDictionary来解析json对象。

  1. 使用Newtonsoft.Json而不是jsonSerializer更多的文章来使用。
  2. 继续http://json2csharp.com/并生成你的类来定义你json(复制json和结果是C#类)。
  3. 现在将你的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; }
        }
    }
    

答案 1 :(得分:1)

&#34; user.login_url&#34;是您希望在JavaScript中使用的属性路径...尝试访问字典键而不是

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

答案 2 :(得分:1)

itemsList [“user”]包含第二个词典。因此,您可以使用

向下导航到login_url变量
var user = (IDictionary<string, object>)itemsList["user"];
var login_url = user["login_url"];

答案 3 :(得分:0)

尝试使用http://json.net/它会为您提供一个包含所需类型的词典..