省市区 【构建对象】转换成json

省市区 构建对象转换成json

using NewOjbect.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace NewOjbect.Controllers
{
    public class HomeController : Controller
    {
        salesEntities db = new salesEntities();
        JavaScriptSerializer jsz = new JavaScriptSerializer();
        //
        // GET: /Home/

        public ActionResult Index()
        {
            ViewData["abc"] = Pro_city();
            return View();
        }

        public string Pro_city()
        {
            var list_pro = db.S_Province.ToList(); //省
            var list_city = db.S_City.ToList();  //市
            var list_District = db.S_District.ToList(); //区县


            var result = from f in list_pro //遍历省,构建对象
                         //遍历市,构建对象
                         select new
                         {
                             proName = f.ProvinceName,
                             cities = (from c in list_city
                                       where c.ProvinceID == f.ProvinceID
                                       select new
                                       {
                                           //遍历区县(其实遍历的结果就是一个“区县名”的list集合)
                                           c.CityName,
                                           disName = (from d in list_District where c.CityID == d.CityID select d.DistrictName)
                                       })
                         };

            var s = new { jsonData = result }; //再构建一个自定义对象
            string strJson = jsz.Serialize(s);//将这个对象转换成一个json

            return strJson;
        }

    }
}
省市区三个表

转成的json的结果



上面的构建的对象仅仅是包含省名,市名,区名称。同样我们构建的对象也可以包含三个表所有的数据:如下

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

using NewOjbect.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace NewOjbect.Controllers
{
    public class HomeController : Controller
    {
        salesEntities db = new salesEntities();
        JavaScriptSerializer jsz = new JavaScriptSerializer();
        //
        // GET: /Home/

        public ActionResult Index()
        {
            ViewData["abc"] = pro_cityTow();
            return View();
        }

        //上面的构建的对象仅仅是包含省名,市名,区名称。同样我们构建的对象也可以包含三个表所有的数据
        public string pro_cityTow()
        {
            var list_pro = db.S_Province.ToList(); //省
            var list_city = db.S_City.ToList();  //市
            var list_District = db.S_District.ToList(); //区县

            //遍历省
            var result = from f in list_pro
                         //构建对象
                         select new
                         {
                             proName = f,
                             cities = (from c in list_city  //遍历市
                                       where c.ProvinceID == f.ProvinceID
                                       select new //构建对象
                                       {

                                           c,
                                           //遍历区县(其实遍历的结果就是一个“区县”的list集合)
                                           disName = (from d in list_District where c.CityID == d.CityID select d)
                                       })
                         };

            var s = new { jsonData = result }; //再构建一个自定义对象
            string strJson = jsz.Serialize(s);//将这个对象转换成一个json

            return strJson;

        }
    }
}

最终转成的json效果



 public string getCountryDepJson()
        {
            List<CR_BC_PROVINCE> list_Province = db.CR_BC_PROVINCE.ToList();            //省
            List<CR_BC_CITY> list_City = db.CR_BC_CITY.ToList();                        //市
            List<CR_BC_CITY_DISTRICT> list_District = db.CR_BC_CITY_DISTRICT.ToList();  //区
            List<CR_BC_STORE> list_Store = db.CR_BC_STORE.ToList();         //所有门店

            string result = new JObject(
                new JProperty("countryDeps",
                    new JArray(
                        from p in list_Province
                        select new JObject
                            (
                                new JProperty("allDepCount", (
                                    from s in list_Store
                                    join c in list_City on s.CR_BC_CITY_ID equals c.CR_BC_CITY_ID
                                    where c.CR_BC_PROVINCE_ID == p.CR_BC_PROVINCE_ID
                                    select 1
                                    ).Count()),
                                new JProperty("cityCount", (list_City.Where(x => x.CR_BC_PROVINCE_ID == p.CR_BC_PROVINCE_ID).Count())),
                                new JProperty("provinceid", p.CR_BC_PROVINCE_ID),
                                new JProperty("provincename", p.PROVINCE_NAME),
                                new JProperty("provincesimplename", p.PROVINCE_SIMPLE_NAME),
                                new JProperty("cityDeps", new JArray(
                                    from c in list_City
                                    where c.CR_BC_PROVINCE_ID == p.CR_BC_PROVINCE_ID
                                    select new JObject
                                        (
                                            new JProperty("allDistrictCount", 0),   //默认为0
                                            new JProperty("cityenname", c.EN_NAME),
                                            new JProperty("cityid", c.CR_BC_CITY_ID),
                                            new JProperty("cityname", c.CITY_NAME),
                                            new JProperty("depcount", (list_Store.Where(x => x.CR_BC_CITY_ID == c.CR_BC_CITY_ID).Count()))
                                        )
                                    )
                                )
                            )
                        )
                    )
                ,
                new JProperty("municipalityDeps",
                    new JArray(
                        from c in list_City
                        where c.MUNICIPALITY == true
                        select new JObject
                            (
                                new JProperty("allDistrictCount", (list_Store.Where(x => x.CR_BC_CITY_ID == c.CR_BC_CITY_ID).Count())),
                                new JProperty("cityenname", c.EN_NAME),
                                new JProperty("cityid", c.CR_BC_CITY_ID),
                                new JProperty("cityname", c.CITY_NAME),
                                new JProperty("depcount", (list_District.Where(x => x.CR_BC_CITY_ID == c.CR_BC_CITY_ID).Count())),
                                new JProperty("districtDeps", new JArray(
                                    from d in list_District
                                    where c.CR_BC_CITY_ID == d.CR_BC_CITY_ID
                                    select new JObject
                                        (
                                            new JProperty("districtDepcount", (list_Store.Where(x => x.CR_BC_CITY_DISTRICT_ID == d.CR_BC_CITY_DISTRICT_ID).Count())),
                                            new JProperty("districtid", d.CR_BC_CITY_DISTRICT_ID),
                                            new JProperty("districtname", d.CITY_DISTRICT_NAME)
                                        )
                                    )
                                )
                            )
                        )
                    )
                ).ToString();

            return result;
        }


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值