0

What I'am attempt to do is deserialize json and foreach through that object and get Name, Lastname, Id values from it. I think deserializing works but how can I foreach throught thoes values of that object and write out?

ResponseFromServer =

responseFromServer"{\"ReportName\":\"TestReport\",\"Results\":[
{
   \"Name\":\"Test1\",
   \"Lastname\":\"Test2\",
   \"ID\":\"1111111111\",
},{
   \"Name\":\"Test\",
   \"Lastname\":\"Test\",
   \"Id\":\"222222222\"
}
]}" string

Code so far:

object jsonObj = new System.Web.Script.Serialization.JavaScriptSerializer().DeserializeObject(responseFromServer);

var myObject = jsonObj.GetType().GetProperties();

foreach (var obj in myObject)
{
    Console.WriteLine(obj);
}
1
  • 1
    I suggest you use either strong typing to describe the expected object structure and dynamic feature. I haven't worked with MS serialiser, but JSON.NET is perfectly capable of supporting both options I've mentioned. Commented Apr 20, 2017 at 22:18

3 Answers 3

2

I recommend, that you create a class for your JSON, like this:

public class Result
{
    public string Name { get; set; }
    public string Lastname { get; set; }
    public string Id { get; set; }
}

public class ResponseClass
{
    public string ReportName { get; set; }
    public List<Result> Results { get; set; }
}

And with this class, you can convert your object and get the properties in a foreach, like the Lastname.

var yourObjectWithStructure = JsonConvert.DeserializeObject<ResponseClass>(responseFromServer,
            new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });

 foreach (var result in objec.Results)
 {
     var id = result.Id;
     var lastName = result.Lastname;
     var name = result.Name;
 }

Update: You have "ID" and "Id" in your JSON response. I suggest, that "ID" should be "Id". Is that right?

Sign up to request clarification or add additional context in comments.

1 Comment

@user2809327 Could I answer your question? If, can you mark this as accepted, with the hook under the down vote button. It's helpful for the users of stack overflow, to see that your question is answered. And perhaps you can make this: stackoverflow.com/tour
0

I would use newtonsoft and create a concrete object with a List Property

Comments

0

If you download the NewtonSoft framework:

Then you can create a class for your response...

public class Report
{
   public string ReportName {get; set;};
   public string TestReport {get; set;};
   public List<Person> Results {get; set;}
}
public class Person
{
   public string Name {get; set};
   public string LastName {get; set};
   public int ID {get; set;}
}

and you can deserialise directly into a list of that object class like this:

using System.Web.Script.Serialization;
===============

List<Person> persons = JavaScriptSerializer().Deserialize<List<Report>>(responseFromServer);

Foreach(Person p in persons)
{
   //stuff
}

4 Comments

The ReportName is missing in your answer.
Thanks, spotted that just as I hit post...fixing it now :)
I think then it's similar with my answer, but it's okay. ;)
JavaScriptSerializer is not part of Json.Net; it is built into the .NET framework (that is why the namespace starts with System.Web). If you want to use Json.Net, then you should call JsonConvert.DeserializeObject<T> instead. It is in the Newtonsoft.Json namespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.