• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

[MVC_Json序列化]Json字符串反序列化成C#对象

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

上一篇中有Json序列化相关问题得到了解决。

那么结果集为Json串时,如何将Json串转成C#对象呢?

现举例说明:

-现有如下字符串数据

string k = "{\"rings\":["
                +"[[34995.513100000098,4304381.4231000002],[34988.120099999942,4304371.5420999993],"
                + "[34995.513100000098,4304381.4231000002],[34988.120099999942,4304371.5420999993],"
                + "[34995.513100000098,4304381.4231000002],[34988.120099999942,4304371.5420999993]]],"
                + "\"spatialReference\":";

-想将上面的数据转换成List<point>

public class point {
            public decimal x { get; set; }
            public decimal y { get; set; }
        }

 

步骤1:

-截取字符串

public string strCutOut(string str) {
            string str1 = str.Substring(str.IndexOf(":[[") + 3, str.IndexOf("]],") - 11);
            string str2 = str1.Replace("],[", "]$[");
            string str3 = str2.Replace("[", "{'x':");
            string str4 = str3.Replace("]", "}");
            string str5 = str4.Replace(",", ",'y':");
            string str6 = str5.Replace("$", ",");
            string str7 = str6.Replace("'", "\"");
            return str7;
        }

-得到如下字符串

"{\"x\":34995.513100000098,\"y\":4304381.4231000002},

{\"x\":34988.120099999942,\"y\":4304371.5420999993},

{\"x\":34995.513100000098,\"y\":4304381.4231000002},

{\"x\":34988.120099999942,\"y\":4304371.5420999993},

{\"x\":34995.513100000098,\"y\":4304381.4231000002},

{\"x\":34988.120099999942,\"y\":4304371.5420999993}"

 

步骤2:

-引用System.Runtime.Serialization.Json;

-反序列化字符串

public List<point> convertObject(string json)
        {
            MemoryStream stream2 = new MemoryStream();
            DataContractJsonSerializer ser2 = new DataContractJsonSerializer(typeof(List<point>));
            StreamWriter wr = new StreamWriter(stream2);
            wr.Write(json);
            wr.Flush();
            stream2.Position = 0;
            Object obj = ser2.ReadObject(stream2);
            List<point> list = (List<point>)obj;
            return list;
        }

 

步骤3:

-调用字符串截取及反序列化字符串

public void readJson(){

            string k = "{\"rings\":["
                +"[[34995.513100000098,4304381.4231000002],[34988.120099999942,4304371.5420999993],"
                + "[34995.513100000098,4304381.4231000002],[34988.120099999942,4304371.5420999993],"
                + "[34995.513100000098,4304381.4231000002],[34988.120099999942,4304371.5420999993]]],"
                + "\"spatialReference\":";
            string str = strCutOut(k);
            string json = "[" + str + "]";
            List<point> ls = convertObject(json);
        }

 

*得到的结果集ls为:


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
面试问题-C#接口和抽象类的区别发布时间:2022-07-13
下一篇:
C#中遍历目录下的目录的文件发布时间:2022-07-13
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap