.NET: How to pass typed serialize using JavaScriptSerializer ==> Deserialize in C#

MohitPandit
MohitPandit used Ask the Experts™
on
Hello,

I've JSON. Could you assist how to create C# class & pass typed class object in JSON deserialize so that I can get First Name, Last Name & Phone number in array?

I've JSON output in this format

  1. JSON root
HeaderResponse
Response
documents

The data type respectively as below:

1. {} HeaderResponse
i.) Status (integer)
ii.) TimeStamp (integer)

2. {} Response
i.) Number (integer)
ii.) Start (integer)

3. Under (Response)
i.) [documents
With multiple rows like
a.) First Name (string)
b.) Last Name (string)
c.) [] Phone number (string), but can be multiple

Best Regards
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Assuming you mean something like:
{
    HeaderResponse: {
        Status: int,
        TimeStamp: int
    },
    Response {
        Number: int,
        Start, int,
        Documents: [{
            FirstName: string,
            LastName: string,
            PhoneNumbers: [{
               string1,
               string2,
               string3
            }]
        }]
    }
}

Open in new window

Then something like this should suffice:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace EE_Q29009564
{
    class Program
    {
        static string response = "{\"HeaderResponse\":{\"Status\":1,\"TimeStamp\":1},\"Response\":{\"Number\":1,\"Start\":1,\"Documents\":[{\"FirstName\":\"firstName\",\"LastName\":\"lastName\",\"PhoneNumbers\":[\"phone1\",\"phone2\",\"phone3\"]}]}}";
        static void Main(string[] args)
        {
            var serviceResponse = new ServiceResponse()
            {
                HeaderResponse = new HeaderResponse()
                {
                    Status = 1,
                    TimeStamp = 1
                },
                Response = new Response()
                {
                    Number = 1,
                    Start = 1,
                    Documents = new List<Document>() {
                        new Document()
                        {
                            FirstName = "firstName",
                            LastName = "lastName",
                            PhoneNumbers = new List<string>()
                            {
                                "phone1", "phone2", "phone3"
                            }
                        }
                    }
                }
            };
            var serialized =  JsonConvert.SerializeObject(serviceResponse);
            Console.WriteLine(serialized);
            Console.WriteLine();
            Console.WriteLine("The following should be parsable into a new ServiceResponse: {0}", response);
            var parsed = JsonConvert.DeserializeObject(response);
            Console.WriteLine(parsed);
            Console.ReadLine();
        }
    }

    class ServiceResponse
    {
        public HeaderResponse HeaderResponse { get; set; }
        public Response Response { get; set; }
    }

    class HeaderResponse
    {
        public int Status { get; set; }
        public int TimeStamp { get; set; }
    }

    class Response
    {
        public int Number { get; set; }
        public int Start { get; set; }
        public List<Document> Documents { get; set; }

        public Response()
        {
            Documents = new List<Document>();
        }
    }

    class Document
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public List<string> PhoneNumbers { get; set; }

        public Document()
        {
            PhoneNumbers = new List<string>();
        }
    }
}

Open in new window

Which produces the following output -Capture.PNG
-saige-

Author

Commented:
Thank you. I'll implement same at my end and let you know.

Author

Commented:
I implemented same. Once, I got JSON string and pass to JavaScriptSerializer. It is giving me below error:


No parameterless constructor defined for type of 'MyPOCName.Form1+ServiceResponse[]

Please find below code, where it is giving an error:

JavaScriptSerializer sr = new JavaScriptSerializer();
jsonResponse = sr.Deserialize<ServiceResponse[]>(jsonString);

Open in new window


Could you check once?

Best Regards
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Commented:
First, I am using the Newtonsoft library in order to parse the json (IMHO, provides a more robust and complete solution for converting json objects to c# objects).

Second, you are trying to convert a singular json result into an array of json results.

-saige-

Author

Commented:
Okay, I'll make change for removal of array and let you know

Best Regards

Author

Commented:
Thank you

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial