1. 程式人生 > >Newtonsoft.Json取json字串中的值得用法

Newtonsoft.Json取json字串中的值得用法

<%@ WebHandler Language="C#" Class="AddShopOnly" %>

using System;
using System.Web;
using Newtonsoft.Json;//先引入這兩個名稱空間
using Newtonsoft.Json.Converters;
using System.Data;
using Redsz.DAO;
public class AddShopOnly : IHttpHandler
{

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";

string uid = context.Request["uid"];
string projectid = context.Request["projectid"];
string parme = GetJson(uid);
object obj = JsonConvert.DeserializeObject(parme);//obj   轉換json格式的字串為obj物件


//{
// status: 0,
// message: "ok",
// result: {
// name: "江邊城外•巫山烤全魚亞運村店",
// location: {
// lng: 116.412347,
// lat: 40.005328
// },
// address: "朝陽區慧忠北路慧忠裡123號樓(近北辰東路)",
// telephone: "(010)64924119",
// uid: "8ee4560cf91d160e6cc02cd7",
// detail_info: {
// tag: "美食 中餐館 烤魚 飯統北京 家人團聚 朋友聚會 燒烤 學生聚餐 二三人小聚 家庭團聚 生日PARTY 同事朋友聚會 亞運村店 川菜 適合大夥人 餐館 川北涼粉 大拌菜 豆豉烤魚 豆豉清江魚 怪味清江魚 會員卡商戶 家常泡餅 家庭聚會 可以刷卡 麻辣烤魚 免費停車 朋友聚餐 皮蛋豆腐 青椒皮蛋 情侶約會 商務宴請 跳水木耳 無線上網 香辣烤魚 香辣清江魚 休閒小憩 有無煙區",
// detail_url: "http://api.map.baidu.com/place/detail?uid=8ee4560cf91d160e6cc02cd7&output=html&source=placeapi_v2",
// type: "cater",
// price: "63",
// overall_rating: "5.0",
// taste_rating: "5.0",
// service_rating: "5.0",
// environment_rating: "5.0",
// image_num: "671",
// comment_num: "1191",
// favorite_num: "1851",
// checkin_num: "45060",
// shop_hours: "11:00-23:00"
// }
// }
//}

Newtonsoft.Json.Linq.JObject js = obj as Newtonsoft.Json.Linq.JObject;//把上面的obj轉換為 Jobject物件

Newtonsoft.Json.Linq.JToken model = js["result"];//取Jtoken物件     通過Jobject的索引獲得到

string name=model["name"].ToString();//這裡是取值
string lat = model["location"]["lat"].ToString();
string lng = model["location"]["lng"].ToString();
string address = model["address"] == null ? "" : model["address"].ToString();
string telephone = model["telephone"] == null ? "" : model["telephone"].ToString();

Newtonsoft.Json.Linq.JToken detail_info = js["result"]["detail_info"];

string price = detail_info["price"]==null?"":detail_info["price"].ToString();
string detail_url = detail_info["detail_url"].ToString();

string sql = "select count(id) as num from shops where shop_name='"+name+"' and projectid='"+projectid+"'; ";
int res=Convert.ToInt32(Data.getDataTableBySql(sql).Rows[0]["num"]);
if (res>0)
{
context.Response.Write("{\"success\":false,\"msg\":'此商家已新增到資料庫'}");
}
else
{
string shop_pic = "暫無圖片";//根據取到匹配的圖片地址 再上傳到我們自己的伺服器

string createtime = DateTime.Now.ToString();
string insql = string.Format("INSERT shops (projectid,shop_name,shop_address,shop_tel,shop_pic,shop_applogo,shop_lat,shop_lng,createtime,status,state,isopen,upload_flag,active_flag)values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}')",
projectid, name, address, telephone, shop_pic, shop_pic, lat, lng, createtime, 1, 1, 1, 1, 1);
Data.RunSql(insql);
context.Response.Write("{\"success\":true,\"msg\":'新增成功'}");
}


}

public bool IsReusable
{
get
{
return false;
}
}


/// <summary>
/// 返回json格式的資料
/// </summary>
/// <param name="lat">緯度</param>
/// <param name="lng">經度</param>
/// <param name="radius">範圍</param>
/// <param name="page_size">條數</param>
/// <returns></returns>
public string GetJson(string uid)
{

//http://api.map.baidu.com/place/v2/detail?uid=8ee4560cf91d160e6cc02cd7&ak=E4805d16520de693a3fe707cdc962045&output=json&scope=2
// http://api.map.baidu.com/place/v2/search?&query=%E5%B0%8F%E5%90%83%24%E7%90%86%E5%8F%91%E5%BA%97%24%E5%8C%BB%E9%99%A2%24%E7%94%9F%E6%B4%BB%E6%9C%8D%E5%8A%A1%24%E5%95%86%E5%BA%97%24%E8%B6%85%E5%B8%82%24%E4%BA%94%E9%87%91&location=<%=Request["lat"] %>,<%=Request["lng"] %>&radius=10000&output=json&ak=1735edad684487a9c4c82d1a94065632&page_size=10&page_num=0&scope=2
string uri = string.Format("http://api.map.baidu.com/place/v2/detail?uid=" + uid + "&ak=1735edad684487a9c4c82d1a94065632&output=json&scope=2");
System.Net.WebClient wc = new System.Net.WebClient();
byte[] bResponse = wc.DownloadData(uri);
string strResponse = System.Text.Encoding.UTF8.GetString(bResponse);
// {"status":0,"result":{"location":{"lng":115.70974321125,"lat":32.13694390485},"precise":0,"confidence":14,"level":"\u533a\u53bf"}}

return strResponse;
}
}