1

I am trying to deserialize the following into C# classes:

{
    "response" : {
        "" : {
            "Expense" : [[{
                        "chart_of_accounts_id" : "45f2fd8f-68b2-44cc-b07ac031c97cd96c",
                        "account_name" : "Salaries",
                        "amount" : "1500.00",
                        "entry_type" : "Debit",
                        "business_id" : "528f00bb-8cd8-4e7f-be6a-0724c327a7be",
                        "account_category" : "5"
                    }, {
                        "chart_of_accounts_id" : "45f2fd8f-68b2-44cc-b07ac031c97cd96c",
                        "account_name" : "Salaries",
                        "amount" : "200.00",
                        "entry_type" : "Debit",
                        "business_id" : "528f00bb-8cd8-4e7f-be6a-0724c327a7be",
                        "account_category" : "5"
                    }
                ]]
        }
    },
    "messages" : {
        "msgs" : "",
        "errs" : ""
    }
}

I have the following so far but I get the error "cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'Systems.Collections.Generic.List'1[eko_app.Expenses+ExpensesResponse]' because it requires a JSON array (e.g. [1,2,3]) to deserialize correctly"

public class Expense
{
    public string chart_of_accounts_id { get; set; }
    public string account_name { get; set; }
    public decimal amount { get; set; }
    public string entry_type { get; set; }
    public string business_id { get; set; }
    public int account_category { get; set; }
}

public class ExpensesResponse
{
    public List<Expense> Expense { get; set; }
}

public class Messages
{
    public string msgs { get; set; }
    public string errs { get; set; }
}

public class RootObject
{
    public List<ExpensesResponse> response { get; set; }
    public Messages messages { get; set; }
}

// deserialize the json to c# .net
var response = Newtonsoft.Json.JsonConvert.DeserializeObject<RootObject>(jsonData);

if (response != null)
{
          expenses = response.response;
}

What should I do to correct this?

7
  • That's a KeyValuePair. You could use a Dictionary<string, string> for it. Commented Dec 3, 2013 at 13:02
  • Why do you have an empty key ?! Commented Dec 3, 2013 at 13:04
  • @Joffrey Kern, I was just sent some Json Api calls and this is the one that I'm having trouble with. Commented Dec 3, 2013 at 13:07
  • If your requirement is oly to acces the values of json.You can use mongoDB Bson dll and do just BsonDocument.Parse("ypur json string") Then you can get all the values for the key. Check out mongoDb driver Commented Dec 3, 2013 at 13:12
  • 1
    Look at "Expense" : [[{ - there are two [ - so you should try something like List<List<Expense>> Commented Dec 3, 2013 at 13:14

1 Answer 1

2

I have deserialized the data with following types. There was a property with empty name, it should have JsonPropertyAttribute.

public class Expense
{
    public string chart_of_accounts_id { get; set; }
    public string account_name { get; set; }
    public decimal amount { get; set; }
    public string entry_type { get; set; }
    public string business_id { get; set; }
    public int account_category { get; set; }
}

public class ExpensesResponse
{
    [JsonProperty(PropertyName = "")] 
    public ExpensesResponseContent Content { get; set; }
}

public class ExpensesResponseContent
{
    public List<List<Expense>> Expense { get; set; }

}

public class Messages
{
    public string msgs { get; set; }
    public string errs { get; set; }
}

public class RootObject
{
    public ExpensesResponse response { get; set; }
    public Messages messages { get; set; }
}

You can check how your data is structured with Online JSON Viewer.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your answer. Still having trouble with it.
Im getting a null value for the result. However at jsonData = w.DownloadString(expensesURL);, just before desirializing, I am getting valid values.
@KinyanjuiKamau I updated the answer. Can you retry?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.