创建嵌套类类的Javascript JSON [英] Create Javascript JSON of class with nested class

查看:120
本文介绍了创建嵌套类类的Javascript JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要建立在具有嵌套对象的JavaScript JSON对象

I want to create a JSON object in JavaScript that has a nested object.

下面是类:

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 _CellName;

    #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 CellName
    {
        get
        {
            return this._CellName;
        }
        internal set
        {
            this._CellName = value;
        }
    }

    #endregion //Properties

    #region Constructors

    internal CellLocation()
    {

    }

    public CellLocation(int worksheet, string cellName)
    {
        this.Worksheet = worksheet;
        this.CellName = cellName;
        int i = 0;
        string columnRaw = String.Empty;
        string rowRaw = String.Empty;
        int column = 0;
        int row = 0;
        while (Char.IsLetter(this.CellName, i))
        {
            columnRaw += this.CellName.Substring(i, 1);
            i++;
        }
        column = Utilities.Excel.ColumnLetterToNumber(columnRaw);
        rowRaw = this.CellName.Substring(i);
        if (!Int32.TryParse(rowRaw, out row))
            throw new ApplicationException(String.Format("Cell name {0} is invalid", cellName));

        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 name
        this.CellName = String.Concat(Utilities.Excel.ColumnNumberToLetter(column), row + 1);
    }

    #endregion //Constructors

}

这是最后一个字符串我希望输出的样子:

This is the final string I want the output to look like:

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

什么是做到这一点?

如果正确的方法它很重要,我使用的后端Newtonsoft JSON库。

If it matters, I am using the Newtonsoft JSON library on the backend.

推荐答案

这是比我想象的容易多了。

It's a lot easier than I thought.

这是JavaScript:

This is the JavaScript:

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

在服务器端,使用Newtonsoft JSON库反序列化甚至esaier:

On the server side, deserializing using the Newtonsoft JSON library is even esaier:

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

这篇关于创建嵌套类类的Javascript JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆