Reputation: 43883
I have a treeview structure in html. It will only have nodes, and subnodes. It can't have another sublevel. One of the features of this treeview is that users can rearrange the nodes and subnodes.
When the user saves it, I will make it send a JSON object to my c# page, and in the server it will read the new order and save it. The order is determined by the order of the objects in the JSON object.
I picture the JSON object will look like this (example):
[ {id: 4, items: [5, 6, 7]}, {id: 8, items: [9, 10, 11, 12, ...]}, ... ]
So it would be an array of dictionaries. For each dictionary, it will have two keys, the id
will be the id number of the node, and the items
will be an array of the id's of the subnodes.
It's important that the order is the same as the order I add to the array, so that when I iterate through them, its still in the same order when I inserted them. If its like a dictionary then this wont be true.
Im pretty sure I can create this array of dictionaries using jquery. But how can I turn this into a JSON object? Is this possible?
And in c#, how would I be able to read this?
Upvotes: 0
Views: 1543
Reputation: 1537
Namespace
System.Web.Script.Serialization;
Question 1
But how can I turn this into a JSON object? Is this possible?
Answer
JavaScriptSerializer jss = new JavaScriptSerializer();
var jsonObjList = jss .Serialize(objList);
Question 2
And in c#, how would I be able to read this?
Answer
Model
public class Object1
{
public int id {get; set;}
public List<int> items {get; set;}
}
Logic
string jsonData = @"[ {id: 4, items: [5, 6, 7]}, {id: 8, items: [9, 10, 11, 12, ...]}, ... ]";
JavaScriptSerializer jss = new JavaScriptSerializer();
var objList = jss.Deserialize<List<Object1 >>(jsonData);
Upvotes: 1
Reputation: 2989
A simple way of parsing this data in C# would to use a library such as JSON.Net. You are able to parse a json string and traverse through this without having to actually mirror this JSON structure you have to actual C# Classes, however this can become helpful with large amounts of Json.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
static void Main(string[] args)
{
string json = "{ items: [ {id: 4, items: [5, 6, 7]},
{id: 8, items: [9, 10, 11, 12]}, {id: 5, items: [9, 10, 11, 12]} ] }";
JToken root = JToken.Parse(json);
JToken items = root["items"];
var results = from element in items
select new
{
Id = element["id"].Value<int>(),
Items = element["items"].Select(x => x.Value<int>())
};
foreach (var r in results)
{
Console.WriteLine("Id: {0}, Items: {1}", r.Id, string.Join(", ", r.Items));
}
}
Prints to console:
Id: 4, Items: 5, 6, 7
Id: 8, Items: 9, 10, 11, 12
Id: 5, Items: 9, 10, 11, 12
Another handy utility I like to use is Json2C#, which gives me a foundation. But the example I have provided shows you a lightweight, fast approach to retrieving your data after you have sent a HTTP Post request.
Upvotes: 1
Reputation: 20544
_ really don't see the problem. If you're using jQuery you're using Javascript. A JSON object is a Javascript object, so the following would be valid in jQuery. It will serialize the object into a string.
var data = [ {id: 4, items: [5, 6, 7]}, {id: 8, items: [9, 10, 11, 12, ...]}, ... ];
$.post({
data: data,
// ...
});
If you need the string, JSON.Stringify() will turn the object into a string. (I didn't realize it was native to Javascript). Never had too since I've been always using jQuery ajax.
Upvotes: 3