Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deserialize json using json.net

Posted on 2014-04-29
3
Medium Priority
?
491 Views
Last Modified: 2014-04-29
I'm trying to deseriliaze this json response without luck.

Unable to cast object of type 'Newtonsoft.Json.Linq.JArray' to type 'RootObject'.

RootObject rootObject = JsonConvert.DeserializeObject(responseMessage);
What Am I doing wrong?

[{"total_collected_money":{"currency_code":"USD","amount":3497},"tender":[{"type":"CREDIT_CARD","name":"Credit Card","total_money":{"currency_code":"USD","amount":3497},"card_brand":"AMERICAN_EXPRESS","pan_suffix":"9999","entry_method":"SWIPED"}],"inclusive_tax_money":{"currency_code":"USD","amount":0},"id":"xxxx","discount_money":{"currency_code":"USD","amount":-1498},"device":{"name":"iPad"},"net_total_money":{"currency_code":"USD","amount":3401},"tax_money":{"currency_code":"USD","amount":0},"creator_id":"xxxx","merchant_id":"xxxx","refunded_money":{"currency_code":"USD","amount":-13487},"inclusive_tax":[],"refunds":[{"type":"FULL","created_at":"2014-02-20T16:21:32Z","processed_at":"2014-02-20T16:21:32Z","reason":"Canceled Order","refunded_money":{"currency_code":"USD","amount":-3497}}],"additive_tax_money":{"currency_code":"USD","amount":0},"processing_fee_money":{"currency_code":"USD","amount":-96},"tip_money":{"currency_code":"USD","amount":0},"additive_tax":[],"created_at":"2014-02-20T16:20:06Z"}]


public class TotalCollectedMoney
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class TotalMoney
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class Tender
{
    public string type { get; set; }
    public string name { get; set; }
    public TotalMoney total_money { get; set; }
    public string card_brand { get; set; }
    public string pan_suffix { get; set; }
    public string entry_method { get; set; }
}

public class InclusiveTaxMoney
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class DiscountMoney
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class Device
{
    public string name { get; set; }
}

public class NetTotalMoney
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class TaxMoney
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class RefundedMoney
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class RefundedMoney2
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class Refund
{
    public string type { get; set; }
    public string created_at { get; set; }
    public string processed_at { get; set; }
    public string reason { get; set; }
    public RefundedMoney2 refunded_money { get; set; }
}

public class AdditiveTaxMoney
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class ProcessingFeeMoney
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class TipMoney
{
    public string currency_code { get; set; }
    public int amount { get; set; }
}

public class RootObject
{
    public TotalCollectedMoney total_collected_money { get; set; }
    public List<Tender> tender { get; set; }
    public InclusiveTaxMoney inclusive_tax_money { get; set; }
    public string id { get; set; }
    public DiscountMoney discount_money { get; set; }
    public Device device { get; set; }
    public NetTotalMoney net_total_money { get; set; }
    public TaxMoney tax_money { get; set; }
    public string creator_id { get; set; }
    public string merchant_id { get; set; }
    public RefundedMoney refunded_money { get; set; }
    public List<object> inclusive_tax { get; set; }
    public List<Refund> refunds { get; set; }
    public AdditiveTaxMoney additive_tax_money { get; set; }
    public ProcessingFeeMoney processing_fee_money { get; set; }
    public TipMoney tip_money { get; set; }
    public List<object> additive_tax { get; set; }
    public string created_at { get; set; }
}

Open in new window

0
Comment
Question by:JRockFL
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 2000 total points
ID: 40030061
The "root" of your JSON is an array (note the square brackets). Is that how it should be?
0
 
LVL 8

Author Comment

by:JRockFL
ID: 40030096
Yes, it should be an array. I omitted part of the response.
0
 
LVL 8

Author Comment

by:JRockFL
ID: 40030127
That was it.
I have it now. Thank you!

RootObject[] rootobject = JsonConvert.DeserializeObject<RootObject[]>(responseMessage);

Open in new window

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question