本文介绍了如何将原始数据转换为json数据? 。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例:



输出:



原始数据:



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



Json数据:



{

NewsByIdResult:[{NewsID:4,CompanyID:0,CompanyIds:null,CompanyId:160,CompanyIdVal:null,Topic:280 ,行业:83,公司法律名称:TCS商业信息}]





}



我尝试过:



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

返回JsonNews;



使用上面的代码将对象列表转换为Json数据。但我得到的输出原始数据不像json数据。 Kinly帮助我想要json数据。

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.

推荐答案

WebGet(UriTemplate = "NewsById/{NewsID}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]





试试这样:



try it like this:

WebGet(UriTemplate = "NewsById/{NewsID}", ResponseFormat = WebMessageFormat.Json]





它将以这种格式返回数据:

[{ \NewsID \:3,\NewsCompId \:2,\CompanyId \:1,\CompanyLegalName \:\demo 1 \},{\\ \\NewsID \:97,\NewsCompId \:98,\CompanyId \:99,\CompanyLegalName \:\demo 2 \}]



你可以反序列化这个JS轻松打开:



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:

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:

[OperationContract]
        [WebGet(UriTemplate = "WrappedListNewsById/{value}", ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
        List<Newss> GetList(string value);





WCF服务非常智能,可以将整个列表序列化为json,因此您不必使用



The WCF service is smart enough to serialize the whole list into json for you so you don't have to use

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







查看我更新的github示例:

[]


这篇关于如何将原始数据转换为json数据? 。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 07:31