[MVC_Json序列化]Json字串還原序列化成C#對象

來源:互聯網
上載者:User

標籤:

上一篇中有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為:

[MVC_Json序列化]Json字串還原序列化成C#對象

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.