zoukankan      html  css  js  c++  java
  • C#中Object转化为json对象

    比如定义一个类:

    复制代码
    public class Lines
        {
            public string X1 { get; set; }
            public string X2 { get; set; }
            public string Y1 { get; set; }
            public string Y2 { get; set; }
            public string Z1 { get; set; }
            public string Z2 { get; set; }
            public string diameter { get; set; }
            public string material { get; set; }
        }
    复制代码

    然后实例化了许多个Lines对象,存放在List<Lines>中。

    如:

    复制代码
    List<Lines> linesobj = new List<Lines>();
    for (int i = 0; i < 20; i++)
            { 
                linesobj.Add(new Lines(){X1=“1”, X2=“2”, Y1=“3”, Y2=“4”, Z1=”5“, Z2=”6“, diameter=”7“, material=”8”}); 
            }
    复制代码

    linesobj即为一个包含了20个Lines对象的数组。

    下面将linesobj转化为json:

    1.引用命名空间:using System.Web.Script.Serialization;

    2.使用JavaScriptSerializer对象的Serialize方法,本例中为:

    JavaScriptSerializer jss = new JavaScriptSerializer();
    string myJson = jss.Serialize(linesobj);
    return myJson;

    此时的myJson即为转化后的json对象。

    myJson[0]={"X1" : "1","X2" : "2"....};

    myJson[1]={"X1" : "1","X2" : "2"....};

    ...

    myJson[20]=...

  • 相关阅读:
    二叉树(前序,中序,后序遍历)查找
    插入查找
    归并排序
    解密Spring AOP 之AspectJ与动态代理基础知识
    常用的sql
    python 集合方法
    python 字典
    python 列表方法
    python 序列类型
    fake_useragent
  • 原文地址:https://www.cnblogs.com/gq0324/p/8583239.html
Copyright © 2011-2022 走看看