2012-05-03 24 views
1

2つの関連するASP.NET WebサービスのJSONに関する質問がここにあります。私はN層設計のASP.NET 2.0アプリケーションを使用しています。JSON ASP.NET Webサービスを使用した複数のオブジェクトの作成


質問1:
私は、各オブジェクトが返された複数のオブジェクトを持つ複数のオブジェクトのJSONレスポンスを生成するために、私のASP.NET Webサービスを取得しようとしています。私はそれを正しく説明しているかどうか分からないので、私が探しているものの例がここにあります。 id 3のみOldデータセットを有し、id 4のみNewデータセットを有している

[ 
    {"id":1, "CommentDataElement": "Value", 
     "OldData":{ "Description": "ABC", "Status": "0" }, 
     "NewData":{ "ShortText": "Text here", "LongText": "More text here" } 
    }, 
    {"id":2, "CommentDataElement": "Value", 
     "OldData":{ "Description": "DEF", "Status": "1" }, 
     "NewData":{ "ShortText": "Text here", "LongText": "More text here" } 
    }, 
    {"id":3, "CommentDataElement": "Value", 
     "OldData":{ "Description": "GHI", "Status": "2" } 
    }, 
    {"id":4, "CommentDataElement": "Value", 
     "NewData":{ "ShortText": "Text here", "LongText": "More text here" } 
    } 
] 

idの1及び2は、両方OldNewデータセットを持っています。

次に、この応答を処理するためにjQueryを使用します。 OldNewデータセットの存在に基づいて、私は適切なページ要素を生成します。

OldDataNewDataのソースは、2つの異なる表に記載されています。これは可能ですか?


質問2:また
、それは、ASP.NET Webサービスを持つことが可能である二つの異なるオブジェクト構造をJSON文字列を返しますか?例。

[ 
    {...same as above, just cut out for brevity...} 
    {"id":4, "CommentDataElement": "Value", 
     "NewData":{ "ShortText": "Text here", "LongText": "More text here" } 
    }, 
    {"Count":"100"} 
] 

基本的に私が何をしたいのか、サーバに要求を1つ作る私の切り捨てられたデータセット(ページングに基づいて、唯一のフルセットの20件のレコードを返すと言う)を取得し、そのようフルレコードは、カウントを設定しますされます私は自分のページ番号(ページ番号)を生成することができます。私のデータアクセスレイヤーでは、レコードセットに対してreturnステートメントを使用することが可能であり、フルカウントについてはOUTPUTパラメータを使用できますか?ちょっとした考え。


私がここに書いたJSONは適切ではないと確信しています。あなたが適切に見えるように更新してください。どんな助けもありがとう!ありがとう!

答えて

1

この問題を解決できました。

基本的にクラスを作成してから、そのクラスをリストとして作成します。たとえば、...

public class OldData 
{ 
    private string _Description = string.empty; 
    private string _Status = string.empty; 

    public string Description { get {return _Description;} set {_Description = value; } } 
    public string Status { get {return _Status;} set {_Status = value; } } 
} 

public class NewData 
{ 
    private string _ShortText = string.empty; 
    private string _LongText = string.empty; 

    public string ShortText { get {return _ShortText;} set {_ShortText = value; } } 
    public string LongText { get {return _LongText;} set {_LongText = value; } } 
} 

public class Results 
{ 
    private int? _ID = null; 
    private OldData _od = null; 
    private NewData _nd = null; 

    public int? ID { get {return _ID;} set {_ID = value; } } 
    public OldData od { get {return _od;} set {_od = value; } } 
    public NewData nd { get {return _nd;} set {_nd = value; } } 
} 

public class Results_List : List<Results> 
{ 
    public Results_List() { } 
} 

次に、クラスのインスタンスを作成し、データベースのデータを入力できます。 JSONレスポンスは、クライアント側ロジックの適切な形式になります。うまくいけば、これは他の誰かを助けるでしょう楽しい!

関連する問題