0

I'm new in ASP.NET. I have an object tree structure and I want to use Bootstrap-treeview. The problem is that it get json data as input.

What is the best way to convert my List to JSON? Is it better to that in COntroller or better do it in View(JavaScript?)? Mybe there is better ways to build a tree?

Gleb
  • 1,412
  • 1
  • 23
  • 55
  • 1
    Since you are using asp.net-mvc, why don't just use `JsonResult` in `System.Web.Mvc`. Example: `Json(new{data = object});` – Jacky Sep 28 '16 at 06:55

2 Answers2

0

Try this answer ..

JsonConvertion

 String json = "{ \"Id\": 123, \"FirstName\": \"fName\", \"LastName\": \"lName\" }";

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            Student student = serializer.Deserialize<Student>(json);

            List<Student> students=new List<Student>();
            students.Add(student);


            String serializedStudentList = serializer.Serialize(students);
            var d = serializedStudentList;
            List<Student> serializedstudents = serializer.Deserialize<List<Student>>(serializedStudentList);
Community
  • 1
  • 1
Syed Mhamudul Hasan
  • 1,341
  • 2
  • 17
  • 45
0

Suppose you got object like this in list format-

Student: {
    Name:'SomeName',
    Address:'Address',
    Phone:'Phone'
},
.
.
.

Then you create a concrete class with properties as for one of repetitive object -

public class Student{
    public string Name{get;set;}
    public string Address{get;set;}
    public string Phone{get;set;}
}

e.g. Object that you get -

var listObject= new List<Object> 
            {
                new {Name = "Alan", Address = "Doe", Phone = "123456"},
                new {Name = "Alan", Address = "Doe", Phone = "123456"},
            };

Serializing ^ object in Student Form -

using Newtonsoft.Json;

var studentSerialized = JsonConvert.SerializeObject<List<Student>>(listObject);

It will match propert names and will return you List<Student>. Now for treeview you try the same with List type property.

Manoz
  • 6,507
  • 13
  • 68
  • 114