在REST SOE C#中创建一个双序列化JSON对象 [英] Creating a double serialized JSON object in REST SOE C#

查看:122
本文介绍了在REST SOE C#中创建一个双序列化JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否要对我的JSON对象进行两次序列化,但是输出结果是不需要的格式.我正在通过ArcGIS Server对象扩展(REST SOE)公开REST端点.我最近还实现了JSON.Net,这实际上使我可以删除几行代码.

I'm not sure if I'm double-serializing my JSON object, but the output results in an unwanted format. I'm exposing the REST endpoint via an ArcGIS Server Object Extension (REST SOE). I've also recently implemented JSON.Net which essentially allowed me to remove several lines of code.

因此,这里是处理程序,它是为服务创建数据的核心部分(对于非GIS用户而言).

So here is the handler, which is the core piece creating the data for the service (for you non GIS peeps).

private byte[] SearchOptionsResHandler(NameValueCollection boundVariables, string outputFormat, string requestProperties, out string responseProperties)
    {
        responseProperties = null;

        JsonObject result = new JsonObject();                                 

        // Towns
        DataTable dataTableTowns = GetDataTableTowns();
        String jsonStringTowns = JsonConvert.SerializeObject(dataTableTowns); 
        result.AddString("Towns", jsonStringTowns);

        // GL_Description
        DataTable dataTableGLDesc = GetDataTableGLDesc();
        String jsonStringGLDesc = JsonConvert.SerializeObject(dataTableGLDesc);                       
        result.AddString("GLDesc", jsonStringGLDesc);

        return Encoding.UTF8.GetBytes(result.ToJson());
    }

结果是丑陋的转义JSON:

The result is ugly scaped JSON:

{
    "Towns": "[{\"Column1\":\"ANSONIA\"},{\"Column1\":\"BETHANY\"},{\"Column1\":\"BLOOMFIELD\"}]",
    "GLDesc": "[{\"Column1\":\"Commercial\"},{\"Column1\":\"Industrial\"},{\"Column1\":\"Public\"}]"
}

是因为我要以某种方式对它进行两次序列化吗?感谢您的关注.

Is it because I'm double serializing it somehow? Thanks for looking this over.

推荐答案

是的,您正在对其进行双重序列化.就在您的代码中.

Yes, you are double serializing it. It's right there in your code.

对于每个数据表dataTableTownsdataTableGLDesc,您正在调用JsonConvert.SerializeObject()将它们转换为JSON字符串,然后将其添加到结果JsonObject中.然后,您对结果调用ToJson(),这大概会将整个事情再次序列化为JSON.

For each of your data tables, dataTableTowns and dataTableGLDesc, you are calling JsonConvert.SerializeObject() to convert them to JSON strings, which you then add to a result JsonObject. You then call ToJson() on the result, which presumably serializes the whole thing to JSON again.

JsonObject不是Json.Net的一部分,而JsonConvert是Json.Net的一部分,所以我建议使用其中一个.通常,您只想将所有内容累加到单个结果对象中,然后最后将整个对象序列化一次.这是我使用匿名对象保存结果的Json.Net方法:

JsonObject is not part of Json.Net, while JsonConvert is, so I would recommend using one or the other. Generally, you just want to accumulate everything into a single result object and then serialize the whole thing once at the end. Here is how I would do it with Json.Net using an anonymous object to hold the result:

var result = new 
{ 
    Towns = GetDataTableTowns(),
    GLDesc = GetDataTableGLDesc() 
};

string json = JsonConvert.SerializeObject(result);
return Encoding.UTF8.GetBytes(json);

以下是输出:

{
    "Towns":[{"Column1":"ANSONIA"},{"Column1":"BETHANY"},{"Column1":"BLOOMFIELD"}],
    "GLDesc":[{"Column1":"Commercial"},{"Column1":"Industrial"},{"Column1":"Public"}]
}

这篇关于在REST SOE C#中创建一个双序列化JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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