首页 > 代码库 > json字符串转对象

json字符串转对象

1、添加引用

技术分享

2、添加命名空间

using System.Web;
using System.Web.Script.Serialization;

3、

OpenFileDialog fileDialog = new OpenFileDialog();
            fileDialog.Multiselect = true;
            fileDialog.Title = "请选择文件";
            fileDialog.Filter = "(*.txt)|*.txt";
            if (fileDialog.ShowDialog() == DialogResult.OK) 
            {
                string file = fileDialog.FileName;
                string text = System.IO.File.ReadAllText(file, Encoding.Default);    
 
                JavaScriptSerializer js = new JavaScriptSerializer();
                Dictionary<object, object> dic = js.Deserialize<Dictionary<object, object>>(text);
                
                foreach (object key in dic.Keys)
                {
                    int x = Convert.ToInt16(key);
                    int y = Convert.ToInt16(dic[key]);
                    point pt = new point(x, y);
                    pl.Add(pt);
                }
                Point_databind(pl);
             }

使用JObject

1、添加命名空间

using Newtonsoft.Json.Linq;

2、

var obj = JObject.Parse(text);

 

json字符串转对象