1

I am looking to deserialize the below json string to 3 different DataGrids. One for Results, one for withdrawals and one for deposits. Anyone have a good method for doing this? Any help would be much appreciated.

{
  "results": [
    {
      "id": "51142254",
      "tp_id": "!XP4D49X0CD123628",
      "firstname": "Owner",
      "lastname": "Operator",
      "email": "",
      "phone": "",
      "enrolled": "1",
      "balance": 247.54,
      "fleet": "Test Express",
      "deposits": [
        {
          "id": "184022380",
          "date": "2016-02-17",
          "amount": "200.00",
          "transID": "135246",
          "memo": "Scheduled Deposit",
          "status": "Cleared"
        },
        {
          "id": "184022383",
          "date": "2016-02-25",
          "amount": "200.00",
          "transID": "246357",
          "memo": "Scheduled Deposit",
          "status": "Cleared"
        },
        {
          "id": "184022386",
          "date": "2016-03-02",
          "amount": "200.00",
          "transID": "975468",
          "memo": "Scheduled Deposit",
          "status": "Cleared"
        }
      ],
      "withdrawals": [
        {
          "id": "184026949",
          "date": "2016-03-09",
          "amount": "352.46",
          "transID": "395920",
          "memo": "Invoice\r\n\r\n100234",
          "status": "Cleared"
        }
      ]
    },
    {
      "id": "51142326",
      "tp_id": "!XP4D49X7CD123612",
      "firstname": "Owner",
      "lastname": "Operator",
      "email": "",
      "phone": "",
      "enrolled": "1",
      "balance": 0,
      "fleet": "Test\r\nExpress",
      "deposits": [],
      "withdrawals": []
    }
  ]
}  

When i go to json2chsarp.com this is what gets generated for my classes. I am assuming the deposits and withdrawals section are not showing up because the child nodes are not listed here. how should this be done correctly?

public class Result
{
    public string id { get; set; }
    public string tp_id { get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
    public string enrolled { get; set; }
    public double balance { get; set; }
    public string fleet { get; set; }
    public List<object> deposits { get; set; }
    public List<object> withdrawals { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
}  
5
  • My first thought would be to use XML as an intermediate step. Deserializing JSON to XML is simple enough, then take that data and use it to build your data grids. Commented May 6, 2016 at 14:57
  • Deserialize to an object collection, and bind the object collection to a grid. There are lots of tutorials available on deserializing JSON to an object model. Commented May 6, 2016 at 15:00
  • 1
    @Prof.Bear Why in the world would you do that.... There is nothing in XML that is of any more help than json.net Commented May 6, 2016 at 15:03
  • I would deserialize into a object as normal, then create dependency properties that use linq and filter the root property. Commented May 6, 2016 at 15:05
  • Seems to be a bug with json2csharp.com. I manually eliminated the second Result from the JSON -- the one with the empty "deposits": [] and "withdrawals": [] arrays -- and was able to get a proper data model. Commented May 6, 2016 at 18:38

4 Answers 4

1

You have to create a class Results and define each property your JSON has. And also you have to define a class for deposits and for withdrawals, and put in your result class a property List and List. Then you can use this

var results = new JavaScriptSerializer().Deserialize<List<results>>();

Hope this helps

After this you can use your object to fill your datagrids

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

2 Comments

To test quick i did the following code. All values come back as "nothing" Dim jsonstring As String = syncClient.DownloadString(url) Dim javaScriptSerializer As New JavaScriptSerializer() Dim p2 As Withdrawal = javaScriptSerializer.Deserialize(Of Withdrawal)(jsonstring) TextBox1.Text = p2.amount
the json string is in my original post. i used json2csharp.com to create my classes.
1

First, I would suggest creating an object (and subsequent ones) that maps directly to the objects and properties given in the Json string.

You can then use JsonConvert.DeserializeObject<T>("your json string here"); where T is your new object type, to convert it back into an instance of your new object.

Don't forget to use Newtonsoft.Json in your class.

Head over to Newtonsoft's Website to see how to do this/how it works.

I woud also take a look at another useful answer, with a similar question.

Hope this helps.

Comments

1

The Newtonsoft JSON library is an excellent choice for dealing with JSON objects. Using the dynamic features with JObject.Parse, you can pull the data from the JSON object after parsing it using (where rawJson is your JSON string):

    dynamic jsonBody = JObject.Parse(rawJson);
    dynamic results = jsonBody.results;

Looping through the results array, you can grab all of the fields and data you need for inserting into your data grids:

    foreach(dynamic curResult in results)
    {
      JArray deposits = (JArray)curResult.deposits;
      JArray withdrawals = (JArray)curResult.withdrawals;

      //To get column names and values for your first data grid, iterate over the properties within curResult record
      JObject header = (JObject)curResult;
      var headerObjects = header.Properties().
          Find(x => 
              !x.Name.Equals("deposits") ||
              !x.Name.Equals("withdrawals"));
    }

You can use the records in the deposits array and the withdrawals array in each result to populate those data grids.

This is just one of the many ways this library lets you iterate over a JSON document. I hope this is helpful.

Comments

0
public class Deposite
{
    public int id { get; set; }
    public date date { get; set; }
    public double amount { get; set; }
    public int transID { get; set; }
    public string Memo { get; set; }
    public string status { get; set; }
}

public class Withdrawal
{
    public int id { get; set; }
    public date date { get; set; }
    public double amount { get; set; }
    public int transID { get; set; }
    public string Memo { get; set; }
    public string status { get; set; }
}

public class results
{
    public int id { get; set; }
    public int tp_id{ get; set; }
    public string firstname { get; set; }
    public string lastname { get; set; }
    public string email { get; set; }
    public string phone { get; set; }
    public string enrolled { get; set; }
    public string balance { get; set; }
    public string fleet { get; set; }
    public Deposite[] deposite { get; set; }
    public Withdrawal[] withdrawal { get; set; }
}

public class opt
{
    public results[] response { get; set; }
}

using System.Web.Script.Serialization;

JavaScriptSerializer objJS = new JavaScriptSerializer();
opt objopt  = new opt();
objopt  = objJS .Deserialize<opt >("Your String");

5 Comments

this works great for the Results portion. how could I use this to pull in the withdrawals or deposits?
hi steve, try above code this will definitely helps you.
Thank you! i think we are getting close but for some reason still doesn't show up. only object variable is m_response and response. I would also like to add I am using vb.net, so all your code i converted to vb
I was able to get it working now. issue with the classes. Now on to passing to gridview. How can i just pass the Deposits?
just get the data of deposit in list and assign in gridview datasource.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.