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);

}
MGot90
  • 2,422
  • 4
  • 15
  • 31
  • 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. – user12864 Nov 06 '14 at 05:03
  • Check this, It may works for you too: http://stackoverflow.com/questions/26547890/system-collections-generic-list-from-cs-to-js-variable/26549074#26549074 – Amer Zafar Nov 06 '14 at 05:27
  • A better approach would be to use WebAPI, then you simply return the `List` and the framework handles converting it to Json (or Xml) according to the request. – Ian Mercer Nov 06 '14 at 05:57

3 Answers3

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"}, {....}, {....}]
  • 1
    this won't create an object with a People array, which is the desired result. Pop it into an anonymous object before serializing. – iceburg Nov 06 '14 at 05:17
  • 1
    @iceburg, Not sure exactly what OP wants. Title says _List of objects_ and _Wanted Response_ is not valid –  Nov 06 '14 at 05:47
  • @iceburg You're correct but this works fine. It will just use indexs rather than a "People" array attribute. – MGot90 Nov 06 '14 at 15:39
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});
iceburg
  • 1,768
  • 3
  • 17
  • 25
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

Will Wang
  • 537
  • 5
  • 7