Click here to Skip to main content
15,886,798 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
Example:

Output:

Raw Data:

{"NewsByIdResult":"[{\"NewsID\":4,\"CompanyID\":0,\"CompanyIds\":null,\"CompanyId\":160,\"CompanyIdVal\":null,\"Topic\":\"280\",\"Industry\":83,\"CompanyLegalName\":\"TCS BUSINESS INFORMATION\"}]"}

Json Data:

{
"NewsByIdResult": "[{"NewsID":4,"CompanyID":0,"CompanyIds":null,"CompanyId":160,"CompanyIdVal":null,"Topic":"280","Industry":83,"CompanyLegalName":"TCS BUSINESS INFORMATION"}]"


}

What I have tried:

var JsonNews = Newtonsoft.Json.JsonConvert.SerializeObject(Lstnews);
return JsonNews;

Using above code for converting List of Objects to Json Data. But i'm getting the output raw data only not like json data. Kinly help i would like json data.
Posted
Updated 20-Jun-16 1:55am
Comments
F-ES Sitecore 17-Jun-16 7:37am    
Does it matter? As long as it is valid json that's what counts.
Thiyagu Arockiasamy 17-Jun-16 8:02am    
Okay, then how can to remove the \ symbol in raw data. i couldn't able to bind the value.

F-ES Sitecore 17-Jun-16 9:07am    
The "\" isn't really there, VS just adds it when it is showing you the text to let you know that the quote following is a literal quote and not a quote that denotes the start\end of a string.
Thiyagu Arockiasamy 18-Jun-16 1:36am    
Okay sir but its not working. i implement joson data with HTML page using jquery its not work.i added my HTML page jquery code please help sir

<script type="text/javascript">
$(document).ready(function () {
debugger
$.ajax({
crossDomain: true,
async: true,
cache: false,
type: "GET",
url: 'http://localhost:25012/ProjectRestService.svc/NewsById/4',
data: JSON.stringify(),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
alert(response.d);
},
failure: function (response) {
alert(response.responseText);
}
});
}
);
</script>
Thiyagu Arockiasamy 17-Jun-16 8:04am    
i only got output in raw data i want to need joson data. i used code

var JsonNews = Newtonsoft.Json.JsonConvert.SerializeObject(Lstnews);
return JsonNews;

its only get raw data, but i need json data

{"NewsByIdResult":"[{\"NewsID\":4,\"CompanyID\"Blush | :O ,\"CompanyIds\":null,\"CompanyId\":160,\"CompanyIdVal\":null,\"Topic\":\"280\",\"Industry\":83,\"CompanyLegalName\":\"TCS BUSINESS INFORMATION\"}]"}



1 solution

I know what your problem is now.
the problem is that you use:
C#
WebGet(UriTemplate = "NewsById/{NewsID}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]


try it like this:
C#
WebGet(UriTemplate = "NewsById/{NewsID}", ResponseFormat = WebMessageFormat.Json]


it will return the data in this format:
"[{\"NewsID\":3,\"NewsCompId\":2,\"CompanyId\":1,\"CompanyLegalName\":\"demo 1\"},{\"NewsID\":97,\"NewsCompId\":98,\"CompanyId\":99,\"CompanyLegalName\":\"demo 2\"}]"

and you can deserialize this JSON easily with:
JavaScript
JSON.parse("[{\"NewsID\":3,\"NewsCompId\":2,\"CompanyId\":1,\"CompanyLegalName\":\"demo 1\"},{\"NewsID\":97,\"NewsCompId\":98,\"CompanyId\":99,\"CompanyLegalName\":\"demo 2\"}]")


But what I think, you really wanted is this:
C#
[OperationContract]
        [WebGet(UriTemplate = "WrappedListNewsById/{value}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        List<Newss> GetList(string value);


The WCF service is smart enough to serialize the whole list into json for you so you don't have to use
C#
var JsonNews = Newtonsoft.Json.JsonConvert.SerializeObject(Lstnews);

at all.

View my updated github example on:
GitHub - xszaboj/convertToJsonDemo: convertToJsonDemo[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900