如何将对象转换为JSON与jsonconvert - 无 - 关键qoutations [英] how to convert object to json with jsonconvert - without - key-qoutations

查看:153
本文介绍了如何将对象转换为JSON与jsonconvert - 无 - 关键qoutations的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jsonconvert转换简单对象为JSON像

i use jsonconvert to convert simple objects to json like

JsonConvert.SerializeObject(new { label = "MyLabel1" });



to

{ "label":"MyLabel1" }

但我想拿到钥匙没有报价像

but i want to get the keys without quotation like

{ label: "MyLabel1"}

有没有办法通过使用jsonconvert对象为JSON withoud钥匙-quotations转换?

is there a way to convert objects to json withoud "key"-quotations by using jsonconvert?

推荐答案

这预计JSON或实际的JavaScript符号用于创建对象(这是JSON的一个超集)应该能正常使用引号的任何库。

Any library that expects JSON or actual JavaScript notation for creating objects (which is a superset of JSON) should work fine with quotes.

但是,如果你真的要删除它们,您可以设置 JsonTextWriter.QuoteName 为false。这样做需要编写一些代码, JsonConvert.SerializeObject()使用由手:

But if you really want to remove them, you can set JsonTextWriter.QuoteName to false. Doing this requires writing some code that JsonConvert.SerializeObject() uses by hand:

private static string SerializeWithoutQuote(object value)
{
    var serializer = JsonSerializer.Create(null);

    var stringWriter = new StringWriter();

    using (var jsonWriter = new JsonTextWriter(stringWriter))
    {
        jsonWriter.QuoteName = false;

        serializer.Serialize(jsonWriter, value);

        return stringWriter.ToString();
    }
}

这篇关于如何将对象转换为JSON与jsonconvert - 无 - 关键qoutations的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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