You must Sign In to post a response.
  • Category: Webservices

    Parsing Json Response in C#

    Hi All,

    I am not able to parse the JSON getting it from consuming an web service .

    i am using c# , deserializing the response.content, but not table to get the records inside "orderInvoiceList" ,

    Please guide me how do i capture all the fields.

    Thanks in Advance


    Given below the JSON data :

    {"responseCode":0,"responseMessage":"Success","totalOrders":9,"totalPages":1,"currentPage":1,"orderInvoiceList":{"RWH9":{"orderLocation":"RWH","orderId":"RWH9","orderNo":"RWH9","paymentMethod":"Prepaid","status":"Confirmed","orderDate":"01/03/2019 23:18:35","orderAmount":"129.00","orderCurrency":"INR","conversionRate":"1.00","customerName":"Test123","shipAddress1":"Sector 76 Noida","shipAddress2":null,"shipAddress3":null,"shipCity":null,"shipState":"Delhi","shipCountry":"India","shipPincode":"201307","shipPhone1":"9972462835","shipPhone2":null,"shipEmail1":"test123@customer.com","shipEmail2":null,"billName":"Test123","billAddress1":"Sector 76 Noida","billAddress2":null,"billAddress3":null,"billCity":null,"billState":"Delhi","billCountry":"India","billPincode":"201307","billPhone1":"9972462835","billPhone2":null,"billEmail1":"test123@customer.com","billEmail2":null,"orderRemarks":null,"shippingCharges":"0.00","cODCharge":"0.00","handlingCharge2":"0.00","storeCredit":"0.00","isVerifiedOrder":"1","isOnHold":"0","giftvoucher":"0.00","otherDiscount":"0.00","discountCode":null,"cancelRemark":null,"isGiftwrap":"0","giftwrapMsg":"","deliverySlot":"-1","customDataFeld1":"","customDataFeld2":"","customDataFeld3":"","customDataFeld4":"","customDataFeld5":"","discountAmount":"0.000","taxAmount":"13.82","updatedDate":"01/03/2019 23:19:13","delivery_no":null,"invoice_no":null,"items":[{"lineno":"1","sku":"ROOKIE","orderQty":"1.00","cancelledQty":"0.00","shippedQty":"0.00","returnQty":"0.00","unitPrice":"129.000","discountAmt":"0.000","taxAmount":"0.000"}]},"RWH7":{"orderLocation":"RWH","orderId":"RWH7","orderNo":"RWH7","paymentMethod":"Prepaid","status":"Confirmed","orderDate":"01/03/2019 17:37:40","orderAmount":"200.00","orderCurrency":"INR","conversionRate":"1.00","customerName":"Test123","shipAddress1":"Sector 76 Noida","shipAddress2":null,"shipAddress3":null,"shipCity":null,"shipState":"Delhi","shipCountry":"India","shipPincode":"201307","shipPhone1":"9972462835","shipPhone2":null,"shipEmail1":"test123@customer.com","shipEmail2":null,"billName":"Test123","billAddress1":"Sector 76 Noida","billAddress2":null,"billAddress3":null,"billCity":null,"billState":"Delhi","billCountry":"India","billPincode":"201307","billPhone1":"9972462835","billPhone2":null,"billEmail1":"test123@customer.com","billEmail2":null,"orderRemarks":null,"shippingCharges":"0.00","cODCharge":"0.00","handlingCharge2":"0.00","storeCredit":"0.00","isVerifiedOrder":"1","isOnHold":"0","giftvoucher":"0.00","otherDiscount":"0.00","discountCode":null,"cancelRemark":null,"isGiftwrap":"0","giftwrapMsg":"","deliverySlot":"-1","customDataFeld1":"","customDataFeld2":"","customDataFeld3":"","customDataFeld4":"","customDataFeld5":"","discountAmount":"0.000","taxAmount":"21.43","updatedDate":"01/03/2019 17:46:19","delivery_no":null,"invoice_no":null,"items":[{"lineno":"1","sku":"SANDY","orderQty":"1.00","cancelledQty":"0.00","shippedQty":"0.00","returnQty":"0.00","unitPrice":"200.000","discountAmt":"0.000","taxAmount":"0.000"}]},"RWH5":{"orderLocation":"RWH","orderId":"RWH5","orderNo":"RWH5","paymentMethod":"Prepaid","status":"Confirmed","orderDate":"01/03/2019 16:58:20","orderAmount":"120.00","orderCurrency":"INR","conversionRate":"1.00","customerName":"Test123","shipAddress1":"Sector 76 Noida","shipAddress2":null,"shipAddress3":null,"shipCity":null,"shipState":"Delhi","shipCountry":"India","shipPincode":"201307","shipPhone1":"9972462835","shipPhone2":null,"shipEmail1":"test123@customer.com","shipEmail2":null,"billName":"Test123","billAddress1":"Sector 76 Noida","billAddress2":null,"billAddress3":null,"billCity":null,"billState":"Delhi","billCountry":"India","billPincode":"201307","billPhone1":"9972462835","billPhone2":null,"billEmail1":"test123@customer.com","billEmail2":null,"orderRemarks":null,"shippingCharges":"0.00","cODCharge":"0.00","handlingCharge2":"0.00","storeCredit":"0.00","isVerifiedOrder":"1","isOnHold":"0","giftvoucher":"0.00","otherDiscount":"0.00","discountCode":null,"cancelRemark":null,"isGiftwrap":"0","giftwrapMsg":"","deliverySlot":"-1","customDataFeld1":"","customDataFeld2":"","customDataFeld3":"","customDataFeld4":"","customDataFeld5":"","discountAmount":"0.000","taxAmount":"12.86","updatedDate":"01/03/2019 16:59:22","delivery_no":null,"invoice_no":null,"items":[{"lineno":"1","sku":"DAYA","orderQty":"1.00","cancelledQty":"0.00","shippedQty":"0.00","returnQty":"0.00","unitPrice":"120.000","discountAmt":"0.000","taxAmount":"0.000"}]},"RWH17":{"orderLocation":"RWH","orderId":"RWH17","orderNo":"RWH17","paymentMethod":"Prepaid","status":"Confirmed","orderDate":"02/03/2019 12:19:29","orderAmount":"400.00","orderCurrency":"INR","conversionRate":"1.00","customerName":"Test123","shipAddress1":"Sector 76 Noida","shipAddress2":null,"shipAddress3":null,"shipCity":null,"shipState":"Delhi","shipCountry":"India","shipPincode":"201307","shipPhone1":"9972462835","shipPhone2":null,"shipEmail1":"test123@customer.com","shipEmail2":null,"billName":"Test123","billAddress1":"Sector 76 Noida","billAddress2":null,"billAddress3":null,"billCity":null,"billState":"Delhi","billCountry":"India","billPincode":"201307","billPhone1":"9972462835","billPhone2":null,"billEmail1":"test123@customer.com","billEmail2":null,"orderRemarks":null,"shippingCharges":"0.00","cODCharge":"0.00","handlingCharge2":"0.00","storeCredit":"0.00","isVerifiedOrder":"1","isOnHold":"0","giftvoucher":"0.00","otherDiscount":"0.00","discountCode":null,"cancelRemark":null,"isGiftwrap":"0","giftwrapMsg":"","deliverySlot":"-1","customDataFeld1":"","customDataFeld2":"","customDataFeld3":"","customDataFeld4":"","customDataFeld5":"","discountAmount":"0.000","taxAmount":"42.86","updatedDate":"02/03/2019 12:19:37","delivery_no":null,"invoice_no":null,"items":[{"lineno":"1","sku":"26D842","orderQty":"1.00","cancelledQty":"0.00","shippedQty":"0.00","returnQty":"0.00","unitPrice":"400.000","discountAmt":"0.000","taxAmount":"42.860"}]},"RWH6":{"orderLocation":"RWH","orderId":"RWH6","orderNo":"RWH6","paymentMethod":"Prepaid","status":"Confirmed","orderDate":"01/03/2019 17:12:44","orderAmount":"700.00","orderCurrency":"INR","conversionRate":"1.00","customerName":"Test123","shipAddress1":"Sector 76 Noida","shipAddress2":null,"shipAddress3":null,"shipCity":null,"shipState":"Delhi","shipCountry":"India","shipPincode":"201307","shipPhone1":"9972462835","shipPhone2":null,"shipEmail1":"test123@customer.com","shipEmail2":null,"billName":"Test123","billAddress1":"Sector 76 Noida","billAddress2":null,"billAddress3":null,"billCity":null,"billState":"Delhi","billCountry":"India","billPincode":"201307","billPhone1":"9972462835","billPhone2":null,"billEmail1":"test123@customer.com","billEmail2":null,"orderRemarks":null,"shippingCharges":"0.00","cODCharge":"0.00","handlingCharge2":"0.00","storeCredit":"0.00","isVerifiedOrder":"1","isOnHold":"0","giftvoucher":"0.00","otherDiscount":"0.00","discountCode":null,"cancelRemark":null,"isGiftwrap":"0","giftwrapMsg":"","deliverySlot":"-1","customDataFeld1":"","customDataFeld2":"","customDataFeld3":"","customDataFeld4":"","customDataFeld5":"","discountAmount":"0.000","taxAmount":"75.00","updatedDate":"01/03/2019 17:31:37","delivery_no":null,"invoice_no":null,"items":[{"lineno":"1","sku":"VEER","orderQty":"1.00","cancelledQty":"0.00","shippedQty":"0.00","returnQty":"0.00","unitPrice":"700.000","discountAmt":"0.000","taxAmount":"0.000"}]},"RWH1":{"orderLocation":"RWH","orderId":"RWH1","orderNo":"RWH1","paymentMethod":"Prepaid","status":"Confirmed","orderDate":"01/03/2019 15:06:56","orderAmount":"1200.00","orderCurrency":"INR","conversionRate":"1.00","customerName":"eRetail Test","shipAddress1":"wewew","shipAddress2":null,"shipAddress3":null,"shipCity":null,"shipState":"Delhi","shipCountry":"India","shipPincode":"110022","shipPhone1":"9999999999","shipPhone2":null,"shipEmail1":"eretaildevteam@gmail.com","shipEmail2":null,"billName":"eRetail Test","billAddress1":"wewew","billAddress2":null,"billAddress3":null,"billCity":null,"billState":"Delhi","billCountry":"India","billPincode":"110022","billPhone1":"9999999999","billPhone2":null,"billEmail1":"eretaildevteam@gmail.com","billEmail2":null,"orderRemarks":null,"shippingCharges":"0.00","cODCharge":"0.00","handlingCharge2":"0.00","storeCredit":"0.00","isVerifiedOrder":"1","isOnHold":"0","giftvoucher":"0.00","otherDiscount":"0.00","discountCode":null,"cancelRemark":null,"isGiftwrap":"0","giftwrapMsg":"","deliverySlot":"-1","customDataFeld1":"","customDataFeld2":"","customDataFeld3":"","customDataFeld4":"","customDataFeld5":"","discountAmount":"0.000","taxAmount":"128.57","updatedDate":"01/03/2019 15:07:05","delivery_no":null,"invoice_no":null,"items":[{"lineno":"1","sku":"TEST","orderQty":"10.00","cancelledQty":"0.00","shippedQty":"0.00","returnQty":"0.00","unitPrice":"120.000","discountAmt":"0.000","taxAmount":"0.000"}]},"RWH12":{"orderLocation":"RWH","orderId":"RWH12","orderNo":"RWH12","paymentMethod":"Prepaid","status":"Confirmed","orderDate":"02/03/2019 00:50:16","orderAmount":"120.00","orderCurrency":"INR","conversionRate":"1.00","customerName":"Test123","shipAddress1":"Sector 76 Noida","shipAddress2":null,"shipAddress3":null,"shipCity":null,"shipState":"Delhi","shipCountry":"India","shipPincode":"201307","shipPhone1":"9972462835","shipPhone2":null,"shipEmail1":"test123@customer.com","shipEmail2":null,"billName":"Test123","billAddress1":"Sector 76 Noida","billAddress2":null,"billAddress3":null,"billCity":null,"billState":"Delhi","billCountry":"India","billPincode":"201307","billPhone1":"9972462835","billPhone2":null,"billEmail1":"test123@customer.com","billEmail2":null,"orderRemarks":null,"shippingCharges":"0.00","cODCharge":"0.00","handlingCharge2":"0.00","storeCredit":"0.00","isVerifiedOrder":"1","isOnHold":"0","giftvoucher":"0.00","otherDiscount":"0.00","discountCode":null,"cancelRemark":null,"isGiftwrap":"0","giftwrapMsg":"","deliverySlot":"-1","customDataFeld1":"","customDataFeld2":"","customDataFeld3":"","customDataFeld4":"","customDataFeld5":"","discountAmount":"0.000","taxAmount":"12.86","updatedDate":"02/03/2019 00:51:52","delivery_no":null,"invoice_no":null,"items":[{"lineno":"1","sku":"DAYA","orderQty":"1.00","cancelledQty":"0.00","shippedQty":"0.00","returnQty":"0.00","unitPrice":"120.000","discountAmt":"0.000","taxAmount":"0.000"}]},"RWH13":{"orderLocation":"RWH","orderId":"RWH13","orderNo":"RWH13","paymentMethod":"Prepaid","status":"Confirmed","orderDate":"02/03/2019 00:56:47","orderAmount":"120.00","orderCurrency":"INR","conversionRate":"1.00","customerName":"Test123","shipAddress1":"Sector 76 Noida","shipAddress2":null,"shipAddress3":null,"shipCity":null,"shipState":"Delhi","shipCountry":"India","shipPincode":"201307","shipPhone1":"9972462835","shipPhone2":null,"shipEmail1":"test123@customer.com","shipEmail2":null,"billName":"Test123","billAddress1":"Sector 76 Noida","billAddress2":null,"billAddress3":null,"billCity":null,"billState":"Delhi","billCountry":"India","billPincode":"201307","billPhone1":"9972462835","billPhone2":null,"billEmail1":"test123@customer.com","billEmail2":null,"orderRemarks":null,"shippingCharges":"0.00","cODCharge":"0.00","handlingCharge2":"0.00","storeCredit":"0.00","isVerifiedOrder":"1","isOnHold":"0","giftvoucher":"0.00","otherDiscount":"0.00","discountCode":null,"cancelRemark":null,"isGiftwrap":"0","giftwrapMsg":"","deliverySlot":"-1","customDataFeld1":"","customDataFeld2":"","customDataFeld3":"","customDataFeld4":"","customDataFeld5":"","discountAmount":"0.000","taxAmount":"12.86","updatedDate":"02/03/2019 00:57:11","delivery_no":null,"invoice_no":null,"items":[{"lineno":"1","sku":"DAYA","orderQty":"1.00","cancelledQty":"0.00","shippedQty":"0.00","returnQty":"0.00","unitPrice":"120.000","discountAmt":"0.000","taxAmount":"0.000"}]},"RWH10":{"orderLocation":"RWH","orderId":"RWH10","orderNo":"RWH10","paymentMethod":"Prepaid","status":"Pending","orderDate":"02/03/2019 00:09:08","orderAmount":"129.00","orderCurrency":"INR","conversionRate":"1.00","customerName":"Test123","shipAddress1":"Sector 76 Noida","shipAddress2":null,"shipAddress3":null,"shipCity":null,"shipState":"Delhi","shipCountry":"India","shipPincode":"201307","shipPhone1":"9972462835","shipPhone2":null,"shipEmail1":"test123@customer.com","shipEmail2":null,"billName":"Test123","billAddress1":"Sector 76 Noida","billAddress2":null,"billAddress3":null,"billCity":null,"billState":"Delhi","billCountry":"India","billPincode":"201307","billPhone1":"9972462835","billPhone2":null,"billEmail1":"test123@customer.com","billEmail2":null,"orderRemarks":null,"shippingCharges":"0.00","cODCharge":"0.00","handlingCharge2":"0.00","storeCredit":"0.00","isVerifiedOrder":"1","isOnHold":"0","giftvoucher":"0.00","otherDiscount":"0.00","discountCode":null,"cancelRemark":null,"isGiftwrap":"0","giftwrapMsg":"","deliverySlot":"-1","customDataFeld1":"","customDataFeld2":"","customDataFeld3":"","customDataFeld4":"","customDataFeld5":"","discountAmount":"0.000","taxAmount":"13.82","updatedDate":"02/03/2019 00:10:35","delivery_no":null,"invoice_no":null,"items":[{"lineno":"1","sku":"ROOKIE","orderQty":"1.00","cancelledQty":"0.00","shippedQty":"0.00","returnQty":"0.00","unitPrice":"129.000","discountAmt":"0.000","taxAmount":"0.000"}]}}}
  • #769843
    Hi Vinay,
    You can make use of newton json converter (nuget package) to parse your json.

    Sample:
    // First define your json content as properties in a model class as below:

    public class Students
    {
    public List<StudentInformation> data { get; set; }
    }

    public class StudentInformation
    {
    public string success { get; set; }
    public string terms { get; set; }
    public string privacy { get; set; }
    public string timestamp { get; set; }
    public string source { get; set; }
    }

    --- JSON PARSER -----

    using System.Linq;
    using Newtonsoft.Json;
    using Newtonsoft.Json.Linq;

    static void Main(string[] args)
    {
    string json = @"your json content";
    Students students = JsonConvert.DeserializeObject<Students>(json);
    foreach (var item in students.data)
    {
    Console.WriteLine("id: {0}, name: {1}", item.source, item.terms);
    Console.ReadLine();
    }
    }

    Hope it helps you...

  • #769848
    well its an complex json, "RWH000000037" these values are coming dynamically, i am not able to read it. please guide

    {
    "responseCode": 0,
    "responseMessage": "Success",
    "totalOrders": 2,

    "orderInvoiceList": {
    "RWH000000037": {
    "orderLocation": "RWH",
    "orderId": "RWH32",
    "orderNo": "RWH32",
    "delivery_no": "RWH000000037",
    "invoice_no": "RWH-2018/19-023",
    "items": [
    {
    "lineno": "1",
    "sku": "214653",
    "orderQty": "1.00"

    },
    {
    "lineno": "2",
    "sku": "214286",
    "orderQty": "1.00"

    }

    ]
    },
    "RWH000000036": {
    "orderLocation": "RWH",
    "orderId": "RWH31",
    "orderNo": "RWH31",
    "delivery_no": "RWH000000036",
    "invoice_no": "RWH-2018/19-022",
    "items": [
    {
    "lineno": "1",
    "sku": "468436",
    "orderQty": "1.00"

    }
    ]
    }
    }
    }

  • #769855
    Hi Vinay,

    This is a VS2017 project, console app. Newtonsoft added through "Tools/NuGet Package Manager/Manage NuGet Packages for Solution...". Your data from the post is in the file "C:\Projekt\JSONtest\test.json". (Projekt is Swedish and means Project in English).

    This way you get a raw view of the json data, the rest is up to you.

    using System;
    using System.IO;
    using Newtonsoft.Json;

    namespace JSONtest
    {
    class Program
    {
    static void Main(string[] args)
    {
    string json = File.ReadAllText(@"C:\Projekt\JSONtest\test.json");

    JsonTextReader reader = new JsonTextReader(new StringReader(json));
    while (reader.Read())
    {
    if (reader.Value != null)
    {
    Console.WriteLine("Token: {0}, Value: {1}", reader.TokenType, reader.Value);
    }
    else
    {
    Console.WriteLine("Token: {0}", reader.TokenType);
    }
    }
    }
    }
    }

  • #770382
    assume below is the JSON string i have and i will be converting using
    Newtonsoft is also known as Json.NET. It is a high-performance JSON framework for .NET. It is very easy to use and much faster than the built-in JSON serializes of .NET.
    Using JsonConvert class has a method to convert to and from JSON string, SerializeObject() and DeserializeObject() respectively. It can be used where we won't to convert to and from a JSON string.

    I have used "JsonConvert.DeserializeObject" method to cast my JSONobject to my custom class object. Here JSON key name must match with the class property name and matching is case insensitive

    string jsonData = "{ \"FirstName\":\"Ramana",\"LastName\":\"Reddy\" }";
    var myDetails = JsonConvert.DeserializeObject < MyDetail > (jsonData);
    Console.WriteLine(string.Concat("Hi ", myDetails.FirstName, " " + myDetails.LastName));
    Console.ReadLine();
    }
    public class MyDetail
    {
    public string FirstName {
    get;
    set;
    }
    public string LastName {
    get;
    set;
    }
    }

    DataContractJsonSerializerjsonSerializer = newDataContractJsonSerializer(typeof(MyDetail));
    MemoryStream stream = newMemoryStream(Encoding.UTF8.GetBytes(jsonData));
    stream.Position = 0;
    MyDetaildataContractDetail = (MyDetail) jsonSerializer.ReadObject(stream);
    Console.WriteLine(string.Concat("Hi ", dataContractDetail.FirstName, " " + dataContractDetail.LastName));
    Console.ReadLine();

    Thanks!
    B.Ramana Reddy


  • Sign In to post your comments