1, Entity to Json
1.1. Preparation
Let’s take the following Person class as an example, which contains common data types:
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime Birthday { get; set; }
public bool IsVIP { get; set; }
public float Account { get; set; }
public string[] Favorites { get; set; }
public string Remark { get; set; }}Copy the code
Create a Person instance:
Person person = new Person
{
ID = 1,
Name = "Zhang",
Birthday = DateTime.Parse("2000-01-02"),
IsVIP = true,
Account = 12.34f,
Favorites = new string[] { "Eat"."Sleep"}};Copy the code
1.2, Entity to Json
Returns an unindent Json string
JsonConvert.SerializeObject(person)
Copy the code
Returns:
{"ID": 1,"Name":"Zhang"."Birthday":"2000-01-02T00:00:00"."IsVIP":true."Account": 12.34."Favorites": ["Eat"."Sleep"]."Remark":null}
Copy the code
Returns an indented Json string
JsonConvert.SerializeObject(person, Formatting.Indented);
Copy the code
Returns:
{
"ID": 1,
"Name": "Zhang"."Birthday": "2000-01-02T00:00:00"."IsVIP": true."Account": 12.34."Favorites": [
"Eat"."Sleep"]."Remark": null
}
Copy the code
Indent the Json string
private string JsonIndentation(string str)
{
//string str = JsonConvert.SerializeObject(entity);
JsonSerializer serializer = new JsonSerializer();
TextReader tr = new StringReader(str);
JsonTextReader jtr = new JsonTextReader(tr);
object obj = serializer.Deserialize(jtr);
if(obj ! =null)
{
StringWriter textWriter = new StringWriter();
JsonTextWriter jsonWriter = new JsonTextWriter(textWriter)
{
Formatting = Formatting.Indented,
Indentation = 4,
IndentChar = ' '
};
serializer.Serialize(jsonWriter, obj);
return textWriter.ToString();
}
else
{
returnstr; }}Copy the code
Or:
private string JsonIndentation(string json)
{
JObject obj = JObject.Parse(json);
return obj.ToString();
}
Copy the code
Other Settings
JsonSerializerSettings settings = new JsonSerializerSettings();
// Set the date format
settings.DateFormatString = "yyyy-MM-dd";
// Ignore null values
settings.NullValueHandling = NullValueHandling.Ignore;
/ / the indentation
settings.Formatting = Formatting.Indented;
JsonConvert.SerializeObject(person, settings);
Copy the code
Returns:
{
"ID": 1,
"Name": "Zhang"."Birthday": "2000-01-02"."IsVIP": true."Account": 12.34."Favorites": [
"Eat"."Sleep"]}Copy the code
1.3, Json to Entity
JsonConvert.DeserializeObject<Person>(json);
Copy the code
Linq To Json
2.1. Create an object
JObject obj = new JObject();
obj.Add("ID".1);
obj.Add("Name"."Zhang");
obj.Add("Birthday", DateTime.Parse("2000-01-02"));
obj.Add("IsVIP".true);
obj.Add("Account".12.34f);
// Create an array
JArray array = new JArray();
array.Add(new JValue("Eat"));
array.Add(new JValue("Sleep"));
obj.Add("Favorites", array);
obj.Add("Remark".null);
Copy the code
Create JObject from Json string
string json = "{\ \" ID ": 1, \" Name \ ": \" zhang SAN \ ", \ "Birthday \" : \ "the 2000-01-02 T00:00:00 \", \ "IsVIP \" : true, \ "the Account \" : 12.34, \ "Favorites \" : [\ "eating \", \ "Sleep \], \" Remark \ ": null}";
JObject obj = JObject.Parse(json);
Copy the code
Create JObject from Entity
JObject obj = JObject.FromObject(person);
Copy the code
Create JObject with anonymous objects
JObject obj = JObject.FromObject(new { name = "jack", age = 18 });
Copy the code
Display:
{
"name": "jack"."age"18} :Copy the code
2.2. Get the value
int id;
if (obj["ID"] != null)
id = obj["ID"].Value<int> ();Copy the code
Access to an array
Newtonsoft.json.linq does not support fetching arrays directly, but you can fetch lists and convert them to arrays.
string[] favorites;
if (obj["Favorites"] != null)
favorites = obj["Favorites"].Value<List<string>>().ToArray();
Copy the code