1

我有以下 JSON:

{ "Test": {"Header": {"Description": "OK", "Status": "000", "Timestamp": "1338805752" }, "Results": {"Sport": {"Country": { "League": [ {"Name": "ECB 40 League", "TargetURL": "\/Cricket\/International\/ECB 40 League", "Total": "5" }, {"Name": "Friends Life T20 2012", "TargetURL": "\/Cricket\/International\/Friends Life T20 2012", "Total": "0" }, {"Name": "Sri Lanka vs Pakistan (ODI Series)", "TargetURL": "\/Cricket\/International\/Sri Lanka vs Pakistan (ODI Series)", "Total": "0" }, {"Name": "Sri Lanka vs Pakistan 1st ODI", "TargetURL": "\/Cricket\/International\/Sri Lanka vs Pakistan 1st ODI", "Total": "1" }, {"Name": "Test Series", "TargetURL": "\/Cricket\/International\/Test Series", "Total": "1" } ], "Name": "International", "TargetURL": "\/Cricket\/International", "Total": "6" }, "Name": "Cricket", "Total": "5" } } }}

当我从我的 C# 代码中返回它时,我是这样得到的:

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">{ "HotOdds": {"Header": {"Description": "OK", "Status": "000", "Timestamp": "1338805752" }, "Results": {"Sport": {"Country": { "League": [ {"Name": "ECB 40 League", "TargetURL": "\/Cricket\/International\/ECB 40 League", "Total": "5" }, {"Name": "Friends Life T20 2012", "TargetURL": "\/Cricket\/International\/Friends Life T20 2012", "Total": "0" }, {"Name": "Sri Lanka vs Pakistan (ODI Series)", "TargetURL": "\/Cricket\/International\/Sri Lanka vs Pakistan (ODI Series)", "Total": "0" }, {"Name": "Sri Lanka vs Pakistan 1st ODI", "TargetURL": "\/Cricket\/International\/Sri Lanka vs Pakistan 1st ODI", "Total": "1" }, {"Name": "Test Series", "TargetURL": "\/Cricket\/International\/Test Series", "Total": "1" } ], "Name": "International", "TargetURL": "\/Cricket\/International", "Total": "6" }, "Name": "Cricket", "Total": "5" } } }}</string>

这是函数的定义:

[OperationContract(Name = "GetHOSports")]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
string GetSportsList(DateTime date, int sportID);

我不明白为什么我要在 XML 元素中获取 JSON,有人吗?

4

2 回答 2

0

你不应该返回一个带有 json 的字符串,返回你想要序列化的对象json。您还应该能够删除ResponseFormat它的默认设置是json.

看看这个答案:[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

解释如何让你的脚本方法工作。

如果您没有对象模型来序列化它,那么您将很难使用 WCF。它是围绕将 .net 对象序列化为 json 或 xml 的原则设计的。

如果你有 json 字符串,我会选择像Nancy这样的东西,它可以让你更自由地做你喜欢的事情:),其中包括如果你想要 json 字符串。

像这样的东西的 nancy 模块看起来像这样:

public class SampleModule : Nancy.NancyModule 
{
    public SampleModule()
    {
        Get["/GetHOSports"] = parameters => {
            Response.ContentType = "application/json"
            Response.Content = s => {
                var sw = new StreamWriter(s) { AutoFlush = true };
                sw.Write("your json here");
            }
        }
    }
}
于 2012-06-04T07:43:52.513 回答
0

你有没有完成序列化:

   TestRootObject testRootObject = new TestRootObject();
    ......................
    ..............YOUR CODE..............
    ..................
    System.Web.Script.Serialization.JavaScriptSerializer oSerializer = 
             new System.Web.Script.Serialization.JavaScriptSerializer();
    return oSerializer.Serialize(testRootObject);

你的 C# 类:

public class Header
{
    public string Description { get; set; }
    public string Status { get; set; }
    public string Timestamp { get; set; }
}

public class League
{
    public string Name { get; set; }
    public string TargetURL { get; set; }
    public string Total { get; set; }
}

public class Country
{
    public List<League> League { get; set; }
    public string Name { get; set; }
    public string TargetURL { get; set; }
    public string Total { get; set; }
}

public class Sport
{
    public Country Country { get; set; }
    public string Name { get; set; }
    public string Total { get; set; }
}

public class Results
{
    public Sport Sport { get; set; }
}

public class Test
{
    public Header Header { get; set; }
    public Results Results { get; set; }
}

public class TestRootObject
{
    public Test Test { get; set; }
}
于 2012-06-04T07:49:17.380 回答