Newtonsoft.Json:无法将Json数据转换为C#对象



对基于REST的API的调用返回JSON格式的me数据(存储在变量strJSONStringFromAPI中(。

{
   "id": "551", 
   "name": "Dev D", 
   "work": [
      {
         "employer": {
            "name": "Microsoft Corporation"
         }, 
         "position": {
            "name": "Software Development"
         }
      }
   ], 
   "gender": "male"}

我已经创建了以下对应于上述JSON数据的类

public class Employer
{
    private string _name;
    public string name
    {
        get { return _name; }
        set { _name = value; }
    }
}
public class Position
{
    private string _name;
    public string name
    {
        get { return _name; }
        set { _name = value; }
    }
}
public class Work
{
    private Employer _employer;
    private Position _position;
    public Employer employer
    {
        get { return _employer; }
        set { _employer = value; }
    }
    public Position position
    {
        get { return _position; }
        set { _position = value; }
    }
}

 public class UserInfo
    {
        private string _id;
        private string _name;
        private Work[] _wk;
        public string id
        {
            get { return _id; }
            set { _id = value; }
        }
        public string name
        {
            get { return _name; }
            set { _name = value; }
        }
        public Work[] work
        {
            get { return _wk; }
            set { _wk = value; }
        }
    }

现在我有了GetUserInfo方法,它应该返回对象UserInfo,如下所示

Public UserInfo GetUserDetails()
{
   UserInfo user = New UserInfo();
   user  = Newtonsoft.Json.JsonConvert.DeserializeObject<UserInfo>(strJSONStringFromAPI);
   return user;
}

稍后我将访问值作为

label1.text = user.ID ;
label2.text = user.name;

到目前为止,我正在将上述用户对象的所有属性设置为NULL(user.ID=NULL等(我知道我错过了一些非常重要的东西。。有人能帮我在雇主、职位和工作类中还有什么需要做的吗?这样我就可以获得正确的值(例如user.ID="551"等(

您上面的Work类将不会编译。

public class Work
{
    private Employer _employer;
    private Position _position;
    public Employer employer
    {
        get { return _employer; }
        set { _employer = value; }
    }
    public Position position
    {
        get { return _employer; }
        set { _employer = value; }
    }
}

Position属性不能使用_eemployer

用更正后的代码测试了您的代码,它按预期运行。下面是一个使用HTTP处理程序的简单测试:

<%@ WebHandler Language="C#" Class="JsonDotnet" %>
using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
public class JsonDotnet : IHttpHandler {
  public void ProcessRequest (HttpContext context) {
    string json = context.Server.MapPath(
      "~/app_data/json-test.txt"
    );
    UserInfo user = Newtonsoft.Json.JsonConvert
    .DeserializeObject<UserInfo>(
      File.ReadAllText(json)
    );
    context.Response.Write(user.id + "<br>");
    context.Response.Write(user.name + "<br>");
    context.Response.Write(user.work[0].employer.name + "<br>");
  }
  public bool IsReusable {
    get { return false; }
  }
  public class Employer  {
    public string name { get; set;}
  }
  public class Position {
    public string name { get; set;}
  }
  public class Work {
    public Employer employer { get; set;}
    public Position position { get; set;}
  }
  public class UserInfo {
    public string id { get; set;}
    public string name { get; set;}
    public Work[] work { get; set;}
  }
}

不要忘记将json字符串放在~/app_data/json-test.txt.中

您确定strJSONStringFromAPI与您在上面的第一个代码片段中指定的字符串完全相同吗?

反序列化json对象时,您的私有变量应该是公共的,json对象和类中的所有名称都应该相同

相关内容

  • 没有找到相关文章

最新更新