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

前端之家收集整理的这篇文章主要介绍了Newtonsoft.Json取json字符串中的值得用法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

Newtonsoft.Json取json字符串中的值得用法 看红色的部分就可以了


http://www.cnblogs.com/fierceeagle/p/3545615.html

<%@ 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,lat,lng,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;
}
}


原文链接:https://www.f2er.com/json/290153.html

猜你在找的Json相关文章