1

How do you turn a list of Objects into a JSON String?

The code below returns only one attribute, People. How to add multiple attributes to it? I have been using JsonConvert to change an object into JSON format. I would be open other options / opinions on how to do it. Any help would be much appriciated!

Wanted Response:

{"People":
    {"Person": 
        {"FirstName":"Mike", "LastName":"Smith", "Age":"26"}
    },
    {"Person": 
        {"FirstName":"Josh", "LastName":"Doe", "Age":"46"}
    },
    {"Person": 
        {"FirstName":"Adam", "LastName":"Fields", "Age":"36"}
    }
} 

The Person Class

public class Person
{
    public string FirstName { get ;set; }
    public string LastName { get ;set; }
    public int Age { get ;set; }    
}

Processing Logic

public JsonResult GetAllPeople()
{
    List<Person> PersonList = new List<Person>(); 
    String responseJSON = "";

    foreach(string data in something){

        //Some code to get data
        Person p = new Person(); 
        p.FirstName = data.FirstName ;
        p.LastName  = data.LastName 
        p.Age = data.Age;

        responseJSON += new { Person = JsonConvert.SerializeObject(p) };
    }

    return Json(new { People = JsonConvert.SerializeObject(responseJSON ) }, JsonRequestBehavior.AllowGet);

}
3
  • 1
    The exact string you've posted as your "wanted output" is not valid JSON. It has a property identifier followed by three bare objects. Commented Nov 6, 2014 at 5:03
  • Check this, It may works for you too: stackoverflow.com/questions/26547890/… Commented Nov 6, 2014 at 5:27
  • A better approach would be to use WebAPI, then you simply return the List<Person> and the framework handles converting it to Json (or Xml) according to the request. Commented Nov 6, 2014 at 5:57

3 Answers 3

4

Create a list of objects.

List<Person> persons = new List<Person>(); 
persons.Add(new Person { FirstName  = "John", LastName = "Doe" });
// etc
return Json(persons, JsonRequestBehavior.AllowGet);

will return

[{"FirstName":"John", "LastName":"Doe"}, {....}, {....}]
Sign up to request clarification or add additional context in comments.

3 Comments

this won't create an object with a People array, which is the desired result. Pop it into an anonymous object before serializing.
@iceburg, Not sure exactly what OP wants. Title says List of objects and Wanted Response is not valid
@iceburg You're correct but this works fine. It will just use indexs rather than a "People" array attribute.
1

The

return Json()

will actually serialize the object it takes as a parameter. As you are passing in a json string, it's getting double encoded. Create an anonymous object with a property named People, then serialize it. so you can:

return Content(JsonConvert.SerializeObject(new {People=PersonList}))

or

return Json(new {People=PersonList});

Comments

0

you need add a class, we will name it People

public class People{
    public Person Person{set;get;}
}

public JsonResult GetAllPeople()
{
    List<People> PeopleList= new List<People>();
    foreach(string data in something){
    //Some code to get data
    Person p = new Person(); 
    p.FirstName = data.FirstName ;
    p.LastName  = data.LastName 
    p.Age = data.Age;

    PeopleList.Add(new People(){Person = p});
    }        
    return Json(new { People = PeopleList },JsonRequestBehavior.AllowGet);

}

this shall return exactly what you want

Comments

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.