Tuesday, April 19, 2016

C#.net write JSON, (p1)

//準備資料
            string collapse_key = "score_update";
            int time_to_live = 108;
            bool delay_while_idle = true;
            string score = "4x8";
            string time = "15:16.2342";
            List<string> registration_ids = new List<string>() { "4","8","15","16","23","42"};


            //開始拼接字串
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("{");
            sb.AppendLine("\"collapse_key\":\""+collapse_key+"\",");
            sb.AppendLine("\"time_to_live\":"+time_to_live+",");
            sb.AppendLine("\"delay_while_idle\":"+delay_while_idle.ToString().ToLower()+",");
            sb.AppendLine("\"data\":{");
            sb.AppendLine("\"score\":\""+score+"\",");
            sb.AppendLine("\"time\":\""+time+"\"");
            sb.AppendLine("},");
            sb.AppendLine("\"registration_ids\":[");
            foreach (string item in registration_ids)
            {
                sb.Append("\""+item+"\",");
            }
            sb = new StringBuilder(sb.ToString().TrimEnd(','));//移除最後一個「,」字元
            sb.AppendLine("]");
            sb.AppendLine("}");

            //輸出結果
            Response.Write(sb.ToString());
More here:
https://dotblogs.com.tw/shadow/archive/2012/08/16/74099.aspx 

0 Comments:

Post a Comment