2011-10-25 8 views
4

ネストされたオブジェクトを持つJavaScriptでJSONオブジェクトを作成したいとします。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 _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 

} 

これで私は、出力は次のようになりたい、最終的な文字列:

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

これを行うための正しい方法は何ですか?

問題がある場合は、バックエンドでNewtonsoft JSONライブラリを使用しています。

+0

、あなたの質問はそう... C#のように際立って見える書いたものjavascriptについて...あなたは明確にすることができますか? – Crisfole

+0

@Cpfohl javascriptでJSONオブジェクトを作成して、C#オブジェクトに変換したいと考えています。それはもう少し明確になりますか? –

+0

ああ、クール。それははるかに役に立つ!私はこれをどのように使用しているかについて100%明確ではありませんが、[JSON.NET](http://james.newtonking.com/projects/json-net.aspx)を見てみたいと思います。 JSONをC#オブジェクトにシリアライズおよびデシリアライズするためのメソッドを提供します。 (前のページを見てください)。 – Crisfole

答えて

2

私は思ったよりずっと簡単です。

これではJavaScriptです:サーバー側で

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

、Newtonsoft JSONライブラリを使用してデシリアライズすることさえesaierです:

CellChanged cell = JsonConvert.DeserializeObject<CellChanged>(context.Request["CellChanged"]); 
関連する問題