如何获取序列化的Json文件输出 [英] How to get serialized Json file output

查看:66
本文介绍了如何获取序列化的Json文件输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好...我是WCF的新手,但目前正在使用WCF Restful服务.在我的senario中,我需要获取序列化格式的Json文件.这是我的代码示例.

我在app_code中有两个文件,一个是"sample.cs",另一个是"Isample.cs".

内部sample.cs

Hello... I am new to WCF but currently working on WCF Restful service. In my senario i need to get Json file in a serialized format. Here is my code sample.

I have two files inside app_code one is "sample.cs" and another one is "Isample.cs".

Inside sample.cs

public List<Contacts> GetContactListAsJson()
{
  return GetContactList();//this funtion return list<contact> values
}



内部Isample.cs



Inside Isample.cs

[OperationContract]
[WebGet(UriTemplate = "contacts?format=json", ResponseFormat = WebMessageFormat.Json)]
        List<Contacts> GetContactListAsJson();



当我运行此应用程序时,我将得到一个Json文件.当我使用notpad下载打开该Json文件时,格式为



when I run this application I am getting a Json file .When I download opened that Json file using notpad and the format was like

[{"FirstName":"Bala","LastName":"Krishnan","PhoneNo":"91-9898789"},{"FirstName":"Ram","LastName":"Venkat","PhoneNo":"91-9854677"}]



我搜索google和代码项目,发现了一些序列化方法并使用了



I Search google and code project and found some serializing method and used

1. JavascriptSerializer
2. Newtonsoft Json
3. Datacontract Serialization



我也得到了结果,但是就像



I also got result but it was like

[{\"FirstName\":\"Bala\",\"LastName\":\"Krishnan\",\"PhoneNo\":\"91-9898789\"},{\"FirstName\":\"Ram\",\"LastName\":\"Venkat\",\"PhoneNo\":\"91-9854677\"}]



当我使用记事本打开json文件时,我应该怎么做才能得到如下所述的result(d factor).



What should I do to get result(d factor) like below mentioned when i open json file using notepad.

["0001":{
 "FirstName":"Bala",
 "LastName":"Krishnan",
 "PhoneNo":"91-9898789"
},
"0002":{
 "FirstName":"Ram",
 "LastName":"Venkat",
 "PhoneNo":"91-9854677"
}]



在Advance中感谢



Thanks in Advance

推荐答案

下面是JSON服务操作的剪切和粘贴,该服务基本上只接受一个字符串并返回一个字符串,该字符串为JSON UTF-8.在Process 中,我使用LINQ-2-JSON来解析"输入,而不是反序列化"它.您需要阅读文档以确定最佳方法.


Below is a cut and paste of a JSON service operation, which basically just accepts a string and returns a string which is JSON UTF-8. In the Process I use LINQ-2-JSON to ''parse'' the input rather than ''deserialize'' it. You''ll need to read up on the documentation to determine your best approach.


[OperationContract, WebInvoke(Method = "POST", UriTemplate = "/json")]
public Stream JsonHandler(Stream stream)
{
    // Why streams when we really want strings? We have to use them to get the raw data and avoid
    // WCF attempting to interpret parameters for us. Annoying, but necessary.
    using (StreamReader reader = new StreamReader(stream))
    {
        string request = reader.ReadToEnd();

        byte[] resultBytes = Encoding.UTF8.GetBytes(Process(request));
        WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
        return new MemoryStream(resultBytes);
    }
}


好吧,它在第一个实例中返回的Json是有效且正确的,只是没有按照您喜欢的格式进行格式化.因此,它在功能上是正确的.

据我所知,JavascriptSerializer(这是您在此处使用的)没有任何用于格式化输出的选项.

另一种选择是Netwtonsoft Json(JSON.NET),默认情况下,该格式可根据您的需要设置格式.很好.

最好的方法似乎是让您的方法接受Stream输入和输出,然后在您的方法中在JSON.NET上显式调用这些方法.
Well, the Json it returns in the first instance is valid and correct, just not formatted as you like. So, it is functionally correct.

As far as I know the JavascriptSerializer (which is what you''re using here) doesn''t have any options to format output.

An alternative would be Netwtonsoft Json (JSON.NET) which by default formats things as you''d like. It''s good.

The best approach with this seems to be to get your method to take a Stream input and output, and then explicitly call the methods on JSON.NET in your method.


这篇关于如何获取序列化的Json文件输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆