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

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
LVL 6
MohitPanditAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

it_saigeDeveloperCommented:
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-
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
MohitPanditAuthor Commented:
Thank you. I'll implement same at my end and let you know.
0
MohitPanditAuthor 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
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

it_saigeDeveloperCommented:
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-
0
MohitPanditAuthor Commented:
Okay, I'll make change for removal of array and let you know

Best Regards
0
MohitPanditAuthor Commented:
Thank you
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSON

From novice to tech pro — start learning today.