-1

i am getting json result from third party application for integration , it return the value as below. how i can get below json to dataset

{
  "Return": {
    "InvoiceDetails": {
      "Invoiced": "20180930",
      "InvoiceID": "",
      "Amount": "0.00 "
}

Below is the c# code.

using (var streamInvoiceReader = new StreamReader(httpinvoiceResponse.GetResponseStream()))
{
    var responseInv = streamInvoiceReader.ReadToEnd();
    DataSet SetInvoice = JObject.Parse(responseInv)["InvoiceDetails"].ToObject<DataSet>();
    if (SetInvoice != null && SetInvoice.Tables.Count > 0)
    {

        grvInvoice.DataSource = SetInvoice.Tables[0];
    }
}

In responseInv i can see the return string and it is correct but not able to get it in dataset. there is any difference from result to return? How i can get the data from return array? thanks in advance.

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
NAJEEB
  • 251
  • 2
  • 13
  • A `DataSet` is serialized as an object with named `DataTable` properties. A `DataTable` is serialized as an array of rows with name/value pairs. Your JSON doesn't have any arrays at all, so Json.NET has no way of deserializing any portion of it to a `DataTable`. Why do you want a `DataSet` anyway? Why not deserialize to an explicit data model? – dbc Sep 30 '18 at 09:29
  • what is the best way to get the values to c# object, incase there is no arrays? – NAJEEB Sep 30 '18 at 09:33
  • 1
    Well you already have them loaded into a `JObject`, but if that's not good enough, take a look at [How to auto-generate a C# class file from a JSON object string](https://stackoverflow.com/q/21611674). – dbc Sep 30 '18 at 09:56
  • i got minus for this question, there is any reason for that? just a general question. – NAJEEB Oct 03 '18 at 12:45

2 Answers2

0

to solve this problem you should attention to first part of the JSON by [0]

 DataSet SetInvoice = JObject.Parse(responseInv)[0]["InvoiceDetails"].ToObject<DataSet>();
  • Return error , System.ArgumentException: 'Accessed JObject values with invalid key value: 0. Object property name expected.' instead of [0] i tried to pass the ["Return"] also getting error. – NAJEEB Sep 30 '18 at 11:12
0

Above question i got it working as different way, what i did is :

using (var streamInvoiceReader = new StreamReader(httpinvoiceResponse.GetResponseStream()))
{
    var responseInv = streamInvoiceReader.ReadToEnd();
dynamic ResultInvMain = JsonConvert.DeserializeObject(responseInv );
 datetime Invoiced = ResultInvMain .Return.InvoiceDetails.Invoiced;
 string InvID = ResultInvMain .Return.InvoiceDetails.InvoiceID;
}

and work around with the returned data.

NAJEEB
  • 251
  • 2
  • 13