本文介绍了如何使用jsonconvert将对象转换为json-不带-键引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jsonconvert将简单对象转换为json之类的

I use jsonconvert to convert simple objects to json like

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

{ "label":"MyLabel1" }

但是我想获得没有报价的钥匙,如

but i want to get the keys without quotation like

{ label: "MyLabel1"}

是否可以通过使用jsonconvert将带有键"引号的对象转换为json?

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();
    }
}

这篇关于如何使用jsonconvert将对象转换为json-不带-键引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 15:53