We help IT Professionals succeed at work.

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

MohitPandit
MohitPandit asked
on
429 Views
Last Modified: 2017-04-12
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

Developer
CERTIFIED EXPERT
Distinguished Expert 2019
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION

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
it_saigeDeveloper
CERTIFIED EXPERT
Distinguished Expert 2019

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