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; }
}  
Steve
  • 55
  • 1
  • 10
  • 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. – Prof. Bear May 06 '16 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. – DVK May 06 '16 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 – Wobbles May 06 '16 at 15:03
  • I would deserialize into a object as normal, then create dependency properties that use linq and filter the root property. – Wobbles May 06 '16 at 15:05
  • Seems to be a bug with http://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. – dbc May 06 '16 at 18:38

4 Answers4

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

taquion
  • 2,667
  • 2
  • 18
  • 29
  • 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 – Steve May 06 '16 at 15:10
  • the json string is in my original post. i used http://json2csharp.com/ to create my classes. – Steve May 06 '16 at 15:16
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.

Community
  • 1
  • 1
Geoff James
  • 3,122
  • 1
  • 17
  • 36
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.

Gerald LePage
  • 31
  • 1
  • 3
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");
Chetan Sanghani
  • 2,058
  • 2
  • 21
  • 36
  • this works great for the Results portion. how could I use this to pull in the withdrawals or deposits? – Steve May 06 '16 at 15:24
  • hi steve, try above code this will definitely helps you. – Chetan Sanghani May 06 '16 at 15:41
  • 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 – Steve May 06 '16 at 17:59
  • 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? – Steve May 06 '16 at 19:22
  • just get the data of deposit in list and assign in gridview datasource. – Chetan Sanghani May 07 '16 at 07:44