mowatermelon / studyNode

Learning record
MIT License
4 stars 1 forks source link

4.10 #16

Open mowatermelon opened 7 years ago

mowatermelon commented 7 years ago

.net with json download

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
System.Text.Encoding.ASCII.GetString();
char();
public string cutJson(string json,string item,int index)  
{  
    JObject jo = (JObject)JsonConvert.DeserializeObject(json);  
    string value = jo[item][index].ToString();    
    return value;  
}  
<span id="Span1" runat="server"><% =LeftList %>></span>
JArray LeftLists = (JArray)JsonConvert.DeserializeObject(LeftList);

1 第一种


JArray array = new JArray();
array.Add("Manual text");
array.Add(new DateTime(2000, 5, 23));

JObject o = new JObject(); o["MyArray"] = array;

string json = o.ToString(); // { // "MyArray": [ // "Manual text", // "2000-05-23T00:00:00" // ] // } LINQ to JSON


> ### 2 第二种

string json = @"{ 'Name': 'Bad Boys', 'ReleaseDate': '1995-4-7T00:00:00', 'Genres': [ 'Action', 'Comedy' ] }";

Movie m = JsonConvert.DeserializeObject(json);

string name = m.Name; // Bad Boys Deserialize JSON


> ### 3 第三种

Product product = new Product(); product.Name = "Apple"; product.Expiry = new DateTime(2008, 12, 28); product.Sizes = new string[] { "Small" };

string json = JsonConvert.SerializeObject(product); // { // "Name": "Apple", // "Expiry": "2008-12-28T00:00:00", // "Sizes": [ // "Small" // ] // } Serialize JSON

mowatermelon commented 7 years ago
            JavaScriptSerializer js = new JavaScriptSerializer();   //实例化一个能够序列化数据的类
            LeftLists LeftLists = js.Deserialize<LeftLists>(LeftList);    //将json数据转化为对象类型并赋值给list
            string ParentName = LeftLists.ParentName;
            List<dataList> dataList = LeftLists.data;
            int ID = dataList[0].ID;
            string Name = dataList[0].Name;
            string Type = dataList[0].Type;
            string Url = dataList[0].Url; 

        public struct LeftLists
        {
            public string ParentName { get; set; }  //属性的名字,必须与json格式字符串中的"key"值一样。
            public List<dataList> data;//数组处理   
        }

        public struct dataList
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }
            public string Url { get; set; }
        };
mowatermelon commented 7 years ago
            String modules ="<div class=\"panel-group wrap\" id=\"accordion\" role=\"tablist\" aria-multiselectable=\"true\">\n";
            for (int n = 1; n < LeftLs.ToString().Length; n++)
            {
                modules += "<div class='panel' id='panel-"+n+"'>\n";
                modules += "    <div class=\"panel-heading\" role=\"tab\"  id='heading-"+n+"'>\n";
                modules += "        <h4 class=\"panel-title\"><a role=\"button\" data-toggle=\"collapse\" data-parent=\"#accordion\" href='#panel-in-" + n + "' aria-expanded=\"true\" aria-controls='panel-in-" + n + "'>" + LeftLs.ParentName + "</a></h4>\n";
                modules += "    </div>\n";
                modules += "    <div id='panel-in-" + n + "' class=\"panel-collapse collapse\" role=\"tabpanel\" aria-labelledby='panel-in-" + n + "'>\n";
                modules += "        <div class=\"panel-body\">\n";
                modules += "            <div class ='list-group' id='panel-list-"+n+"'>\n";
                for (int m = 1; m < LeftLs.data.ToString().Length; m++)
                {
                    modules += "            <a onclick='open_xml_win('Rlist'," + LeftLs.data[m].Url + ")' class ='list-group-item'>" + LeftLs.data[m].Name + "</a>\n";                              
                }
                modules += "            </div>\n";   
                modules += "        </div>\n";
                modules += "    </div>\n";
                modules += "</div>\n"; 
            }
            modules += "</div>\n"; */
mowatermelon commented 7 years ago

最终版

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Text;
using XXX.Model;
using System.Web.Script.Serialization;
namespace GREI.Suite
{
    public partial class Main : System.Web.UI.Page
    {
        //public string LeftList;
        protected void Page_Load(object sender, EventArgs e)
        {   
            if (!IsPostBack)
            {
                LoadLeftList();
            }
        }

        public void LoadLeftList()
        {
            String LeftList =LoadListXml();
            List<LeftLists> LeftLs = new JavaScriptSerializer().Deserialize<List<LeftLists>>(LeftList);    //将json数据转化为对象类型并赋值给list

            String modules ="<div class=\"panel-group wrap\" id=\"accordion\" role=\"tablist\" aria-multiselectable=\"true\">\n";

            for (int n = 0; n < LeftLs.Count; n++)
            {
                modules += "<div class='panel' id='panel-"+n+"'>\n";
                modules += "    <div class=\"panel-heading\" role=\"tab\"  id='heading-"+n+"'>\n";
                modules += "        <h4 class=\"panel-title\"><a role=\"button\" data-toggle=\"collapse\" data-parent=\"#accordion\" href='#panel-in-" + n + "' aria-expanded=\"true\" aria-controls='panel-in-" + n + "'>" + LeftLs[n].ParentName + "</a></h4>\n";
                modules += "    </div>\n";
                modules += "    <div id='panel-in-" + n + "' class=\"panel-collapse collapse\" role=\"tabpanel\" aria-labelledby='panel-in-" + n + "'>\n";
                modules += "        <div class=\"panel-body\">\n";
                modules += "            <div class ='list-group' id='panel-list-"+n+"'>\n";
                for (int m = 0; m < LeftLs[n].data.Count; m++)
                {
                    modules += "            <a onclick='open_xml_win('Rlist'," + LeftLs[n].data[m].Url + ")' class ='list-group-item'>" + LeftLs[n].data[m].Name + "</a>\n";                              
                }
                modules += "            </div>\n";   
                modules += "        </div>\n";
                modules += "    </div>\n";
                modules += "</div>\n"; 
            }
            modules += "</div>\n";
            leftTree.InnerHtml = modules;
        }

        public string LoadListXml()
        {
            XmlDocument XmlDoc = new XmlDocument();
            string paths = "XXX/XXX.xml";
            paths = Server.MapPath(paths);
            StringBuilder sb = new StringBuilder();
            try
            {
                XmlDoc.Load(paths);

                XmlNode xnroot = XmlDoc.SelectSingleNode("WorkFlow");

                sb.Append("[");
                int index = 0;
                foreach (XmlNode xnType in xnroot.ChildNodes)
                {
                    if (xnType.NodeType == XmlNodeType.Comment)
                    {
                        continue;
                    }

                    if (index > 0)
                    {
                        sb.Append(",");
                    }

                    XmlAttribute attribute = xnType.Attributes["Name"];
                    string parentname =attribute.Value;

                    string strJson = CommonHelper.GetActByProcessName(parentname, paths);

                    string res = "{\"ParentName\":\""+parentname+"\",\"data\":"+strJson+"}";
                    sb.Append(res);
                    index++;
                }
                sb.Append("]");

                return sb.ToString();
            }
            catch (Exception err)
            {
                throw;
            }

        }

        public struct LeftLists
        {
            public string ParentName { get; set; }  //属性的名字,必须与json格式字符串中的"key"值一样。
            public List<dataList> data { get; set; }//数组处理   
        }

        public struct dataList
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public string Type { get; set; }
            public string Url { get; set; }
        };

    }
}