JSON( JavaScript Object Notation) 。 JavaScript 。 JSON , 、 。JSON , , 。
JSON XML
◆
JSON XML , XML , 。
◆
XML , JSON 。 JSON Javascript, Javascript , ,JSON XML 。
JSON
1. JSON JavaScript 。
/ : , 。 : ( ), ( ), ( ), ( ),true/false/null。
:
: , 。
: 。
:
{
"employees": [
{
"firstName": "Bill",
"lastName": "Gates"
},
{
"firstName": "George",
"lastName": "Bush"
}
]
}
2. JSON , 。 "\\" "\"。 :{ "file":"C:\\a.txt"}。
.NET JSON
: JSON , JSON 。 , , 。
JSON ,.NET JSON JSON 。.NET JSON :
LINQ
DataContractJsonSerializer System.Runtime.Serialization.Json
JavaScriptSerializer System.Web.Script.Serialization Web
JsonArray、JsonObject、JsonValue System.Json Silverlight
JsonConvert、JArray、JObject、JValue、JProperty Newtonsoft.Json
2. 【★★★★★】:
Newtonsoft.Json( )。 。 JObject, JsonReader, JsonWriter 。 , , 。
(1) JsonReader Json :
string jsonText = @"{""input"" : ""value"", ""output"" : ""result""}";
JsonReader reader = new JsonTextReader(new StringReader(jsonText));
while (reader.Read())
{
Console.WriteLine(reader.TokenType + "\t\t" + reader.ValueType + "\t\t" + reader.Value);
}
(2) JsonWriter :
StringWriter sw = new StringWriter();
JsonWriter writer = new JsonTextWriter(sw);
writer.WriteStartObject();
writer.WritePropertyName("input");
writer.WriteValue("value");
writer.WritePropertyName("output");
writer.WriteValue("result");
writer.WriteEndObject();
writer.Flush();
string jsonText = sw.GetStringBuilder().ToString();
Console.WriteLine(jsonText);
(3) JObject :
JObject jo = JObject.Parse(jsonText);
string[] values = jo.Properties().Select(item => item.Value.ToString()).ToArray();
(4) JsonSerializer ( JsonWriter JsonReader):
/*--- ---*/
string jsonArrayText1 = "[{'a':'a1','b':'b1'},{'a':'a2','b':'b2'}]";
JArray ja = (JArray)JsonConvert.DeserializeObject(jsonArrayText1);
string ja1a = ja[1]["a"].ToString();
//
JObject o = (JObject)ja[1];
string oa = o["a"].ToString();
/*--- ---*/
string jsonText = "{\"beijing\":{\"zone\":\" \",\"zone_en\":\"haidian\"}}";
JObject jo = (JObject)JsonConvert.DeserializeObject(jsonText);
string zone = jo["beijing"]["zone"].ToString();
string zone_en = jo["beijing"]["zone_en"].ToString();
/*--- Project---*/
Project p = new Project() { Input = "stone", Output = "gold" };
JsonSerializer serializer = new JsonSerializer();
StringWriter sw = new StringWriter();
serializer.Serialize(new JsonTextWriter(sw), p);
Console.WriteLine(sw.GetStringBuilder().ToString());
StringReader sr = new StringReader(@"{""Input"":""stone"", ""Output"":""gold""}");
Project p1 = (Project)serializer.Deserialize(new JsonTextReader(sr), typeof(Project));
Console.WriteLine(p1.Input + "=>" + p1.Output);
Project :
class Project
{
public string Input { get; set; }
public string Output { get; set; }
}
3. : .NET Framework 3.5/4.0 System.Web.Script.Serialization JavaScriptSerializer , 。
Project p = new Project() { Input = "stone", Output = "gold" };
JavaScriptSerializer serializer = new JavaScriptSerializer();
var json = serializer.Serialize(p);
Console.WriteLine(json);
var p1 = serializer.Deserialize<Project>(json);
Console.WriteLine(p1.Input + "=>" + p1.Output);
Console.WriteLine(ReferenceEquals(p,p1));
: VS2010, Target Framework .Net Framework 4, Client Profile。 System.Web.Extensions.dll Web , Console 。
, , 。
4. : System.Runtime.Serialization.dll DataContractJsonSerializer JsonReaderWriterFactory 。
Project p = new Project() { Input = "stone", Output = "gold" };
DataContractJsonSerializer serializer = new DataContractJsonSerializer(p.GetType());
string jsonText;
using (MemoryStream stream = new MemoryStream())
{
serializer.WriteObject(stream, p);
jsonText = Encoding.UTF8.GetString(stream.ToArray());
Console.WriteLine(jsonText);
}
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(jsonText)))
{
DataContractJsonSerializer serializer1 = new DataContractJsonSerializer(typeof(Project));
Project p1 = (Project)serializer1.ReadObject(ms);
Console.WriteLine(p1.Input + "=>" + p1.Output);
}
, Project Attribute:
[DataContract]
class Project
{
[DataMember]
public string Input { get; set; }
[DataMember]
public string Output { get; set; }
}
。
:
/// <summary>
///
/// </summary>
/// <param name="json"></param>
public void PrintInsert()
{
string json_Insert = "{ \"result\": \"true\",\"keys\": {\"orderid\": \"130520142013234\" ,\"mailno\": \"1000000000016\",\"mark\": \" \",\"sitecode\":\" \",\"sitename\":\" \"}}";
string newJson = string.Empty;
string result = string.Empty;
string orderid = string.Empty;
string mailno = string.Empty;
string mark = string.Empty;
string sitecode = string.Empty;
string sitename = string.Empty;
JObject jo = (JObject)JsonConvert.DeserializeObject(json_Insert);
result = jo["result"].ToString();
orderid = jo["keys"]["orderid"].ToString();
mailno = jo["keys"]["mailno"].ToString();
mark = jo["keys"]["mark"].ToString();
sitecode = jo["keys"]["sitecode"].ToString();
sitename = jo["keys"]["sitename"].ToString();
newJson = " :<br/> result:" + result + "<br/> orderid:" + orderid + "<br/> mailno:" + mailno + "<br/>mark:" + mark + "<br/>sitecode:" + sitecode + "<br/> sitename:" + sitename+"<br/><br/>";
Response.Write(newJson);
}