[C#]Json.NET - A high performance Json library
public String NickName { get; set; }
public DateTime Birthday { get; set; }
public int Age
{
get
{
return (int)((DateTime.Now - Birthday).TotalDays / 365);
}
}
}</pre></div>
Person Larry = new Person
{
Name = "Larry Nung",
NickName = "蹂躪",
Birthday = new DateTime(1980,4,19)
};
var json = JsonConvert.SerializeObject(Larry, Formatting.Indented);
...
var person = JsonConvert.DeserializeObject<Person>(json);
...</pre></div>
public String NickName { get; set; }
public DateTime Birthday { get; set; }
public int Age
{
get
{
return (int)((DateTime.Now - Birthday).TotalDays / 365);
}
}
}</pre></div>
public String NickName { get; set; }
public DateTime Birthday { get; set; }
[JsonIgnore]
public int Age
{
get
{
return (int)((DateTime.Now - Birthday).TotalDays / 365);
}
}
}</pre></div>
[JsonProperty]
public String NickName { get; set; }
[JsonProperty]
public DateTime Birthday { get; set; }
public int Age
{
get
{
return (int)((DateTime.Now - Birthday).TotalDays / 365);
}
}
}</pre></div>
[DataMember]
public String NickName { get; set; }
[DataMember]
public DateTime Birthday { get; set; }
public int Age
{
get
{
return (int)((DateTime.Now - Birthday).TotalDays / 365);
}
}
}</pre></div>
json = JsonConvert.SerializeObject(Larry, Formatting.Indented);
Console.WriteLine(json);
...</pre></div>
var linq = from item in JObject.FromObject(dotBlog)["Blogers"]
select item;
foreach (var item in linq)
{
Console.WriteLine(item.ToString());
}</pre></div>
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue("Larry Nung");
jsonWriter.WritePropertyName("NickName");
jsonWriter.WriteValue("蹂躪");
jsonWriter.WriteEndObject();
}
var json = sb.ToString();
Console.WriteLine(json);
...</pre></div>
namespace ConsoleApplication7 { class Program { [DataContract] public class Person { [DataMember] public String Name { get; set; }
[DataMember]
public String NickName { get; set; }
[DataMember]
public DateTime Birthday { get; set; }
public int Age
{
get
{
return (int)((DateTime.Now - Birthday).TotalDays / 365);
}
}
private void ShowMessage(string eventName, StreamingContext context)
{
Console.WriteLine("{0}...", eventName);
Console.WriteLine("Context: {0}", context.Context);
Console.WriteLine("State: {0}", context.State);
Console.WriteLine();
}
[OnSerializing]
internal void OnSerializingMethod(StreamingContext context)
{
ShowMessage("OnSerializingMethod", context);
}
[OnSerialized]
internal void OnSerializedMethod(StreamingContext context)
{
ShowMessage("OnSerializedMethod", context);
}
[OnDeserializing]
internal void OnDeserializingMethod(StreamingContext context)
{
ShowMessage("OnDeserializingMethod", context);
}
[OnDeserialized]
internal void OnDeserializedMethod(StreamingContext context)
{
ShowMessage("OnDeserializedMethod", context);
}
[OnError]
internal void OnError(StreamingContext context, ErrorContext errorContext)
{
ShowMessage("OnError", context);
}
}
static void Main(string[] args)
{
DateTime date = DateTime.Now;
Person Larry1 = new Person
{
Name = "Larry Nung",
NickName = "蹂躪",
Birthday = new DateTime(1980,4,19)
};
var json = JsonConvert.SerializeObject(Larry1, Formatting.Indented);
Console.WriteLine("Serialized json string...");
Console.WriteLine(json);
Console.WriteLine();
var person = JsonConvert.DeserializeObject<Person>(json);
Console.WriteLine("Deserialized person name...");
Console.WriteLine(person.Name);
var Larry2 = new JObject(
new JProperty("Name", "Larry Nung"),
new JProperty("NickName", "蹂躪")
);
json = JsonConvert.SerializeObject(Larry2, Formatting.Indented);
Console.WriteLine("Serialized json string...");
Console.WriteLine(json);
Console.WriteLine();
var dotBlog = new
{
Blogers = new List<Person>{
new Person()
{
Name = "Larry Nung",
NickName = "蹂躪"
},
new Person()
{
Name = "Bill Chung",
NickName = "Bill uncle"
}
}
};
var linq = from item in JObject.FromObject(dotBlog)["Blogers"]
select item;
foreach (var item in linq)
{
Console.WriteLine(item.ToString());
}
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter jsonWriter = new JsonTextWriter(sw))
{
jsonWriter.Formatting = Formatting.Indented;
jsonWriter.WriteStartObject();
jsonWriter.WritePropertyName("Name");
jsonWriter.WriteValue("Larry Nung");
jsonWriter.WritePropertyName("NickName");
jsonWriter.WriteValue("蹂躪");
jsonWriter.WriteEndObject();
}
Console.WriteLine("Serialized json string...");
Console.WriteLine(sb.ToString());
Console.WriteLine();
try
{
person = JsonConvert.DeserializeObject<Person>(@"{""Name"": ""Larry Nung"",""NickName"": [蹂躪,拉力]}",new JsonSerializerSettings()
{
Error = (sender, e)=>
{
Console.WriteLine("Serialize error!!");
e.ErrorContext.Handled = true;
}
});
}
catch
{
}
}
}
}