how to return data with object using wcf json
hello how to get bellow output in wcf json service
please give me suggestion
==============================================
1. "status" which have error & message
2. "data" which have category 's data
==============================================
Case 1 : Now if you are able to find some record from database then
send me that records in "data" & "status" should be like this ,
{
"status": {
"error": "false",
"message": "RESULT_OK"
},
"data": [
{
"cat_id": "1",
"cat_name": "BreakingNews",
"errormessage": ""
},
{
"cat_id": "2",
"cat_name": "MurderNews",
"errormessage": ""
},
{
"cat_id": "3",
"cat_name": "Kidnap",
"errormessage": ""
},
{
"cat_id": "4",
"cat_name": "Rep",
"errormessage": ""
}
]
}
==============================================
Case 2:
you didn't have data in database in that case you have to send me like this ,
{
"status": {
"error": "true",
"message": "RESULT_FAIL"
},
"data": "null" // its optinal ,but good if you send me as null
}
------------------------------------------------------
----------------------------------------------------
My code are bellow
ICategoryServices.cs
[ServiceContract]
public interface ICategoryService
{
[WebInvoke(Method="POST", UriTemplate = "GetCategoryList",RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
CategoryContract[] GetCategoryList();
}
[DataContract]
public class CategoryContract
{
[DataMember]
public string cat_id { get; set; }
[DataMember]
public string cat_name { get; set; }
[DataMember]
public string errormessage { get; set; }
}
CategoryServices.cs
Cls_Category objCategory = new Cls_Category();
public CategoryContract[] GetCategoryList()
{
List<CategoryContract> userdetails = new List<CategoryContract>();
try
{
DataTable dtresult = new DataTable();
dtresult = objCategory.Search(0, "and IsActive=1", "Sequence ASC");
if (dtresult.Rows.Count > 0)
{
for (int i = 0; i < dtresult.Rows.Count; i++)
{
CategoryContract userInfo = new CategoryContract();
{
userInfo.cat_id = dtresult.Rows[i]["CategoryId"].ToString();
userInfo.cat_name = dtresult.Rows[i]["CategoryName"].ToString();
userInfo.errormessage = "";
userdetails.Add(userInfo);
}
}
}
else
{
CategoryContract userInfo = new CategoryContract();
{
userInfo.errormessage = "There is an error retrieving data.";
userdetails.Add(userInfo);
}
}
dtresult.Dispose();
}
catch
{
CategoryContract userInfo = new CategoryContract();
{
userInfo.errormessage = "There is an error retrieving data.";
userdetails.Add(userInfo);
}
}
return userdetails.ToArray();
}