Reputation: 3960
Two related ASP.NET Web Service JSON questions here. I'm using a N-tier designed ASP.NET 2.0 application.
Question 1:
I am trying to get my ASP.NET web service to generate a multiple object JSON response where each object has multiple objects returned. I'm not sure if I am explaining that properly, so here is an example of what I am looking for.
[
{"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
's 1 and 2 have both Old
and New
datasets, while id
3 only has an Old
dataset and id
4 only has a New
dataset.
I would then use jQuery to handle this response. And based on the existence of the Old
and New
datasets, I would generate the proper page elements.
The source of the OldData
and NewData
are in two different tables. So is this possible to do?
Question 2:
In addition, is it possible to have an ASP.NET web service return a JSON string with two different object structures? Example.
[
{...same as above, just cut out for brevity...}
{"id":4, "CommentDataElement": "Value",
"NewData":{ "ShortText": "Text here", "LongText": "More text here" }
},
{"Count":"100"}
]
Basically what I want to do, is make one request to the server, get my truncated dataset (based on paging, say only returning 20 records of the full set) and get the full record set count so that I can generate my page numbers (pagination). On my Data Access Layer, is it possible to use a return statement for the record set and an OUTPUT
parameter for the full count? Just a thought.
I'm pretty sure that the JSON that I have written here is not proper. Please feel free to updated as you see fit. Any help is appreciated! Thanks!
Upvotes: 1
Views: 1013
Reputation: 3960
I was able to resolve this issue.
Basically you create the class, then create the class as a list. For example...
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() { }
}
You then can create an instance of the class and populate it with data from your database. The JSON response is then in the proper format for the client side logic. Hopefully this helps someone else. Enjoy!
Upvotes: 1