3

I have an object model that looks like this:

public class Myclass
{
    public int Id { get; set; }
    public string  name { get; set; }
    public int age { get; set; }
}

public ContentResult GetList(List<Myclass> model)
{

    var list = JsonConvert.SerializeObject(
        model,
        Formatting.Indented,
        new JsonSerializerSettings()
        {
            ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore
        });

    return Content(list, "application/json");
}

I need OUTPUT:

[[1,"name1",23],[2,"name2",30],[3,"name3",26],[4,"name4",29]]
Jim Hewitt
  • 1,726
  • 4
  • 24
  • 26
  • 1
    you need to provide better detail of what you are currently getting...the JSON you showed in your desired output is not valid JSON, and from your code it is likely you are actually getting correct JSON – Robert Petz Jun 02 '17 at 23:39
  • 2
    I should clarify - when I say it is not valid JSON I mean it does not represent JSON that is bound to any kind of object structure. – Robert Petz Jun 02 '17 at 23:42
  • FYI if you don't need any special settings like `ReferenceLoopHandling` (which it looks like you don't), you can `return Json(model);` and not bother with explicit serialization (just return `JsonResult` instead of `ContentResult`) – Paul Abbott Jun 02 '17 at 23:44
  • Your output shows an array of arrays, not an array of objects. – Federico Dipuma Jun 02 '17 at 23:44
  • this method return [{"Id":1,"name1",23},{"Id":2,"name2",30},{"Id":3,"name3",26},{"Id":4,"name4",29},] – Mohamed Abdullah Jun 02 '17 at 23:47
  • i need return value without attribute name like this [{1,"name1",23},{2,"name2",30},{3,"name3",26},{4,"name4",29}] – Mohamed Abdullah Jun 02 '17 at 23:53
  • 1
    that's because the method is returning proper JSON? you want an array of arrays where the arrays are of inconsistent types (string and ints)? you'll probably have to manually generate those strings because there isn't a serializer in the world that will generate that kind of output – Robert Petz Jun 02 '17 at 23:54
  • @MohamedAbdullah https://www.w3schools.com/js/js_json_intro.asp What you are asking for is not JSON – Novaterata Jun 03 '17 at 00:02
  • In your two comments you are NOT writing valid json. – Federico Dipuma Jun 03 '17 at 00:03
  • sorry this method return [{"Id":1,"name":"name1","age":23},{"Id":2,"name":"name2",""age":30},{"Id":3,"name":"name3","age":26}‌​,{"Id":4,"name":"name4","age":29}‌​] – Mohamed Abdullah Jun 03 '17 at 00:03
  • 1
    If what you really want is an array of arrays then convert your list to that type before serialization: `model.Select(m => new object[] { m.Id, m.name, m.age });` – Federico Dipuma Jun 03 '17 at 00:06
  • 1
    @RobertPetz *there isn't a serializer in the world that will generate that kind of output* - Sure? [Computer says no](https://dotnetfiddle.net/XKW2TP) – Sir Rufo Jun 03 '17 at 00:19
  • @FedericoDipuma Thank you for your assistance . ... solved – Mohamed Abdullah Jun 03 '17 at 00:29
  • You could use `ObjectToArrayConverter` from [C# JSON.NET - Deserialize response that uses an unusual data structure](https://stackoverflow.com/a/39462464/3744182). – dbc Jun 03 '17 at 09:15

1 Answers1

-2

you can use this workaround if it satisfy your needs, please let me know if this works

        List<Myclass> model = new List<Myclass>();
        model.Add(new Myclass() { Id = 1, Name = "Name1", Age = 50 });
        model.Add(new Myclass() { Id = 2, Name = "Name2", Age = 51 });
        model.Add(new Myclass() { Id = 3, Name = "Name3", Age = 52 });

        string json = JsonConvert.SerializeObject(model);
        //If you want to replace { with [ and } with ]
        json = json.Replace("{", "[").Replace("}", "]");

        //you can use this workaround to get rid of property names
        string propHeader = "\"{0}\":";

        json= json.Replace(string.Format(propHeader, "Id"), "")
            .Replace(string.Format(propHeader, "Name"),"")
            .Replace(string.Format(propHeader, "Age"), "");

        Console.WriteLine(json);