4

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

Here are the classes:

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}"

What is the correct way to do this?

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

3
  • what you wrote look strikingly like C#...your question seems to be about javascript...could you clarify? Commented Oct 25, 2011 at 20:57
  • @Cpfohl I want to create a JSON object in javascript that I will convert to the C# object. Does that clarify it a bit more? Commented Oct 25, 2011 at 21:08
  • Ahh, cool. That's much more helpful! I'm not 100% clear on how you're using this, but I think you'll want to look at JSON.NET, it provides you with methods for serializing and de-serializing JSON into C# objects. (Check out the front page). Commented Oct 26, 2011 at 12:52

1 Answer 1

2

It's a lot easier than I thought.

This is the JavaScript:

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

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

CellChanged cell = JsonConvert.DeserializeObject<CellChanged>(context.Request["CellChanged"]);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.