2011-10-25 10 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

이 ... 귀하의 질문에 보인다 다음 Newtonsoft JSON 라이브러리를 사용하여 역 직렬화하는 것은 심지어 esaier입니다

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

서버 측에서 :

은 자바 스크립트입니다 자바 스크립트에 관한 ... 당신이 명확히 할 수 있습니까? – Crisfole

+0

@Cpfohl 자바 스크립트에서 JSON 객체를 만들어서 C# 객체로 변환하려고합니다. 그 점이 조금 더 명확 해 졌습니까? –

+0

아, 멋지다. 그것은 훨씬 더 도움이된다! 나는 이것을 어떻게 사용하는지 명확하지 않지만, [JSON.NET] (http://james.newtonking.com/projects/json-net.aspx)을보고 싶을 것입니다. JSON을 C# 객체로 직렬화 및 비 직렬화하는 메소드를 제공합니다. (첫 페이지를 확인하십시오). – Crisfole

답변

2

내가 생각했던 것보다 훨씬 쉽습니다. 당신이 눈에 띄게 C#을 같이 쓴

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