• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
灬伊天?
博客园    首页    新随笔    联系   管理    订阅  订阅

009-Json解析

 1 using MySQL.Album;
 2 using MySQL.Models.Album;
 3 using Newtonsoft.Json;
 4 using System;
 5 using System.Collections.Generic;
 6 using System.Linq;
 7 using System.Web;
 8 
 9 namespace Json.Submit
10 {
11     /// <summary>
12     /// Json_Lyric
13     /// </summary>
14     public class Json_Lyric : IHttpHandler
15     {
16         ORM_Testing ormTesting = new ORM_Testing();
17         public void ProcessRequest(HttpContext context)
18         {
19             context.Response.ContentType = "text/plain";
20             int rows = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : Convert.ToInt32(context.Request["rows"]);
21             int page = string.IsNullOrEmpty(context.Request["page"]) ? 1 : Convert.ToInt32(context.Request["page"]);
22             JsonString js = ormTesting.GetTesting(rows, page);
23             string json = JsonConvert.SerializeObject(js, Formatting.Indented);
24             context.Response.Write(json);
25         }
26 
27         public bool IsReusable
28         {
29             get
30             {
31                 return false;
32             }
33         }
34         public JsonString GetTesting(int rows, int page)
35         {
36             int sum = Convert.ToInt32(SqlHelper.ExecuteScalar("select count(*) from Testing"));
37             int top = rows * page;
38             if (sum % rows > 0 && page == Convert.ToInt32((sum / rows)) + 1)
39             {
40                 rows = sum - rows * (page - 1);
41             }
42             string sql = "select top " + rows + " * from Testing where Id in (select top " + top + " Id from Testing order by Id)order by Id desc;";
43             List<Testing> test = MemoryList(SqlHelper.GetTable(sql), new List<Testing>());
44             JsonString js = new JsonString()
45             {
46                 total = sum,
47                 rows = test.ToArray()
48             };
49             return js;
50         }
51     }
52     public class JsonString
53     {
54         public int total { get; set; }
55         public Array rows { get; set; }
56     }
57 }
 1 namespace ConsoleApplication_Json
 2 {
 3     class Program
 4     {
 5         static void Main(string[] args)
 6         {
 7             string json = "{\"ditu\":[{\"hebei\":\"河北\"},{\"shandong\":\"山东\"},{\"qingdao\":\"青岛\"}] }";
 8             JObject jObj = (JObject)JsonConvert.DeserializeObject(json);
 9             string obj = jObj["ditu"][2]["qingdao"].ToString();
10             Console.WriteLine(obj);
11             Console.ReadKey();
12             string array = jObj["ditu"].ToString();
13             JArray jArr = (JArray)JsonConvert.DeserializeObject(array);
14             string arr = jArr[2]["qingdao"].ToString();
15             Console.WriteLine(arr);
16             Console.ReadKey();
17             string strObj = JsonConvert.SerializeObject(jObj, Formatting.Indented);
18             string strArr = JsonConvert.SerializeObject(jArr, Formatting.Indented);
19             Console.Write(strObj + strArr);
20             Console.ReadKey();
21         }
22     }
23 }

 

posted @ 2018-09-08 20:15  灬伊天☂  阅读(142)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3