[LearnNote] JSON
1 JSON
JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate.
More information is availble on the home page.
2 JSON in JavaScript
2.1 Operate with a JSON object
2.1.1 Create a JSON object
// Declare a JSON object (string => array) var myJSONObject = {"bindings": [ {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"}, {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"}, {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"} ] };
2.1.2 Retrieve the members
myJSONObject.bindings[0].method // "newURI"
2.1.3 Convert a JSON text into an object
Using eval() function
var myObject = eval('(' + myJSONtext + ')');
or using a JSON parser
myData = JSON.parse(text, function (key, value) {
    var type;
    if (value && typeof value === 'object') {
        type = value.type;
        if (typeof type === 'string' && typeof window[type] === 'function') {
            return new (window[type])(value);
        }
    }
    return value;
});
2.1.4 Convert JavaScript data structures into JSON text
function replacer(key, value) { if (typeof value === 'number' && !isFinite(value)) { return String(value); } return value; } var myJSONText = JSON.stringify(myObject, replacer);
Note: you can use node.js to test this.
3 JSon in C#
3.1 Send JSON
// New a object from the anonym class var serverReadyMsg = new { req_type = "serverReady", port = int.Parse(System.Environment.GetEnvironmentVariable("channel_port")) }; var client = new TcpClient(); client.Connect(System.Environment.GetEnvironmentVariable("manager_host_name"), int.Parse(System.Environment.GetEnvironmentVariable("manager_port"))); // Serialize the object JsonSerializerSettings settings = new JsonSerializerSettings(); settings.NullValueHandling = NullValueHandling.Ignore; string json = JsonConvert.SerializeObject(serverReadyMsg, Formatting.None, settings); // Prepare the Data var memoryStream = new MemoryStream(); memoryStream.SetLength(0); memoryStream.Position = 0; // Allocate room for the outgoing size memoryStream.Write(new byte[] { 0, 0, 0, 0 }, 0, 4); System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding(); memoryStream.Write(encoder.GetBytes(json), 0, json.Length); NetworkStream stream = client.GetStream(); Byte[] bytes = BitConverter.GetBytes((uint)(memoryStream.Length - 4)); Byte[] outArray = memoryStream.ToArray(); bytes.CopyTo(outArray, 0); stream.Write(outArray, 0, (int)memoryStream.Length); stream.Flush();
3.2 Parser JSON
string receivedString = string.Empty;
System.Text.UTF8Encoding encoder = new System.Text.UTF8Encoding();
System.Text.Decoder utf8Decode = encoder.GetDecoder();
try
{
    int receivedBufferSize = tcpClient.ReceiveBufferSize;
    if (receivedBufferSize > 0)
    {
        // Retrieve the data from the socket
        byte[] data = new byte[receivedBufferSize];
  
        int dataLength = tcpClient.GetStream().Read(data, 0, data.Length);
  
        char[] receivedChars = new char[dataLength - 4];
        utf8Decode.GetChars(data, 4, dataLength - 4, receivedChars, 0, true);
        receivedString = new String(receivedChars);
  
        // Use Linq to JSON to parse the input
        JObject jobject = JObject.Parse(receivedString);
        JToken token = jobject["req_type"];
  
        string req_type = (string)token;
        
        // Dispatch the request
        switch (req_type)
        {
            case "newServerInstance":
                break;
            case "deleteServerInstance":
                break;
            case "serverReady":
                break;
        }
    }
}
catch (System.IO.IOException)
{
}
3.3 Linq to JSon
Code snippet from : http://blog.csdn.net/zhoufoxcn/article/details/6254657
public static void JsonConvertLinqDemo()  
{  
    User user = new User { UserId = 1, UserName = "周公", CreateDate = DateTime.Now.AddYears(-8), Birthday = DateTime.Now.AddYears(-32), Priority = Priority.Lowest, Salary = 500, Urls = new List<string> { "http://zhoufoxcn.blog.51cto.com", "http://blog.csdn.net/zhoufoxcn" } };  
    //JsonConvert类在Newtonsoft.Json.Net35.dll中,注意到http://www.codeplex.com/json/下载这个dll并添加这个引用  
    //JSON序列化  
    string result = JsonConvert.SerializeObject(user);  
    Console.WriteLine("使用JsonConvert序列化后的结果:{0},长度:{1}", result, result.Length);  
    //使用Linq to JSON  
    JObject jobject = JObject.Parse(result);  
    JToken token = jobject["Urls"];  
    List<string> urlList = new List<string>();  
    foreach (JToken t in token)  
    {  
        urlList.Add(t.ToString());  
    }  
    Console.Write("使用Linq to JSON反序列化后的结果:[");  
    for (int i = 0; i < urlList.Count - 1;i++ )  
    {  
        Console.Write(urlList[i] + ",");  
    }  
    Console.WriteLine(urlList[urlList.Count - 1] + "]");  
}
Post by: Jalen Wang (转载请注明出处)
posted on 2012-02-07 21:14 Jalen Wang 阅读(331) 评论(0) 收藏 举报
                
            
        
浙公网安备 33010602011771号