c# – 使用嵌套类创建类的Javascript JSON

c# – 使用嵌套类创建类的Javascript JSON,第1张

概述我想在 JavaScript中创建一个具有嵌套对象的JSON对象. 以下是课程: public class CellChanged{ private CellLocation _Location = null; private double _CellValue = 0; public CellLocation Location { get 我想在 JavaScript中创建一个具有嵌套对象的JsON对象.

以下是课程:

public class CellChanged{    private CellLocation _Location = null;    private double _CellValue = 0;    public CellLocation Location    {        get        {            return this._Location;        }        set        {            this._Location= value;        }    }    public double CellValue    {        get        {            return this._CellValue;        }        set        {            this._CellValue = value;        }    }}public class CellLocation{    #region Members    private int _Worksheet = 0;    private int _Row = 0;    private int _Column = 0;    private string _Cell@R_419_6889@;    #endregion //Members    #region PropertIEs    public int Worksheet    {        get        {            return this._Worksheet;        }        internal set        {            this._Worksheet = value;        }    }    public int Row    {        get        {            return this._Row;        }        internal set        {            this._Row = value;        }    }    public int Column    {        get        {            return this._Column;        }        set        {            this._Column = value;        }    }    public string Cell@R_419_6889@    {        get        {            return this._Cell@R_419_6889@;        }        internal set        {            this._Cell@R_419_6889@ = value;        }    }    #endregion //PropertIEs    #region Constructors    internal CellLocation()    {    }    public CellLocation(int worksheet,string cell@R_419_6889@)    {        this.Worksheet = worksheet;        this.Cell@R_419_6889@ = cell@R_419_6889@;        int i = 0;        string columnRaw = String.Empty;        string rowRaw = String.Empty;        int column = 0;        int row = 0;        while (Char.IsLetter(this.Cell@R_419_6889@,i))        {            columnRaw += this.Cell@R_419_6889@.Substring(i,1);            i++;        }        column = UtilitIEs.Excel.ColumnLetterToNumber(columnRaw);        rowRaw = this.Cell@R_419_6889@.Substring(i);        if (!Int32.TryParse(rowRaw,out row))            throw new ApplicationException(String.Format("Cell @R_419_6889@ {0} is invalID",cell@R_419_6889@));        this.Row = row - 1;        this.Column = column;    }    [JsonConstructorAttribute]    public CellLocation(int worksheet,int row,int column)    {        this.Worksheet = worksheet;        this.Row = row;        this.Column = column;        //set the cell @R_419_6889@        this.Cell@R_419_6889@ = String.Concat(UtilitIEs.Excel.ColumnNumberToletter(column),row + 1);    }    #endregion //Constructors}

这是我想要输出的最终字符串:

"{\"Location\":{\"Worksheet\":1,\"Row\":2,\"Column\":3},\"CellValue\":4.5}"

这样做的正确方法是什么?

如果重要,我在后端使用Newtonsoft JsON库.

解决方法 这比我想象的容易得多.

这是JavaScript:

var cellChanged = {    "Location": {        "Worksheet": workSheetCurrent,"Row": row,"Column": column        },"CellValue": cellValue};

在服务器端,使用Newtonsoft JsON库进行反序列化甚至更为简单:

CellChanged cell = JsonConvert.DeserializeObject<CellChanged>(context.Request["CellChanged"]);
总结

以上是内存溢出为你收集整理的c# – 使用嵌套类创建类的Javascript JSON全部内容,希望文章能够帮你解决c# – 使用嵌套类创建类的Javascript JSON所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1234069.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存