NoobDeveloper
NoobDeveloper

Reputation: 1887

Newtonsoft.Json : Cant get JSON data to convert into C# Object

Call to a REST based API returns me data in JSON format(stored in variable strJSONStringFromAPI).

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

I have created following classes corresponding to above JSON data

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

        }
    }

Now i have method GetUserInfo which should return object UserInfo as shown below

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

later i will access the values as

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

As of now i am getting all properties of above user object as NULL (user.ID = null etc etc) I know i am missing something very important here..can someone help me what else needs to be done in in Employer , Position and Work classes so that i get proper values (eg user.ID = "551" etc)

Upvotes: 0

Views: 2568

Answers (2)

arpan sharma
arpan sharma

Reputation: 64

Your private variable should be public when you deserialize the json object and all name should be same in both in json object as well as in classes

Upvotes: 0

goTo-devNull
goTo-devNull

Reputation: 9372

Your Work class above will not compile.

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

The Position property can't use _employer.

Tested your code with that corrected, it does work as expected. Here's a simple test using a HTTP Handler:

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

Don't forget to put your json string in ~/app_data/json-test.txt.

Are you sure strJSONStringFromAPI is exactly the same as the string you specified in your first code snippet above?

Upvotes: 1

Related Questions