-
Notifications
You must be signed in to change notification settings - Fork 143
Getting Started
ntotten edited this page Mar 16, 2012
·
5 revisions
JsonObject jsonObject = new JsonObject();
jsonObject["name"] = "foo";
jsonObject["num"] = 10;
jsonObject["is_vip"] = true;
jsonObject["nickname"] = null;
string jsonString = jsonObject.ToString();
Result is: {{ {"name":"foo","num":10,"is_vip":true,"nickname":null} }}
IDictionary<string, object> jsonObject = new Dictionary<string, object>();
jsonObject["name"] = "foo";
jsonObject["num"] = 10;
jsonObject["is_vip"] = true;
jsonObject["nickname"] = null;
string jsonString = SimpleJson.SerializeObject(jsonObject);
Result is: {{ {"name":"foo","num":10,"is_vip":true,"nickname":null} }}
object jsonObject = new { name = "foo", num = 10, is_vip = true, nickname = (object)null };
string jsonString = SimpleJson.SerializeObject(jsonObject);
Result is: {{ {"name":"foo","num":10,"is_vip":true,"nickname":null} }}
JsonArray jsonArray = new JsonArray();
jsonArray.Add("foo");
jsonArray.Add(10);
jsonArray.Add(true);
jsonArray.Add(null);
string json = jsonArray.ToString();
Result is: {{ ["foo",10,true,null] }}
IList<object> jsonArray = new List<object>();
jsonArray.Add("foo");
jsonArray.Add(10);
jsonArray.Add(true);
jsonArray.Add(null);
string json = SimpleJson.SerializeObject(jsonArray);
Result is: {{ ["foo",10,true,null] }}
var jsonArray = new object[] { "foo", 10, true, null };
string json = SimpleJson.SerializeObject(jsonArray);
Result is: {{ ["foo",10,true,null] }}
JsonArray list1 = new JsonArray();
list1.Add("foo");
list1.Add(10);
JsonArray list2 = new JsonArray();
list2.Add(true);
list2.Add(null);
JsonObject obj = new JsonObject();
obj["name"] = "foo";
obj["num"] = 10;
obj["list1"] = list1;
obj["list2"] = list2;
string json = obj.ToString();
Result is: {{ "{"name":"foo","num":10,"list1":["foo",10],"list2":[true,null]}" }}
var list1 = new List<object>();
list1.Add("foo");
list1.Add(10);
var list2 = new List<object>();
list2.Add(true);
list2.Add(null);
var obj = new Dictionary<string, object>();
obj["name"] = "foo";
obj["num"] = 10;
obj["list1"] = list1;
obj["list2"] = list2;
string json = SimpleJson.SerializeObject(obj);
Result is: {{ "{"name":"foo","num":10,"list1":["foo",10],"list2":[true,null]}" }}
Using dynamic
dynamic json = SimpleJson.DeserializeObject(data);
string firstName = json.first_name;
DateTime startDate = json.startDate
int voteCount = json.voteCount
List<object> results = json.results;
Using IDictionary<string, object>
var json = (IDictionary<string, object>)SimpleJson.DeserializeObject(data);
var firstName = (string)json["first_name"];
var startDate = (DateTime)json["startDate"];
var voteCount = (int)json["voteCount"];
var results = (List<object>)json["results"];