简体   繁体   English

如何在 C# 中不使用科学记数法将字符串反序列化为 JObject

[英]How to deserialize string to JObject without scientific notation in C#

I have a string like this:我有一个这样的字符串:

var str = "{'data': {'someProperty': 0.00001}}";

When I parse it to JObject like that当我像那样将它解析为 JObject 时

var jObject = JObject.Parse(str);

My jObject looks like this:我的 jObject 看起来像这样:

{"data": {"someProperty": 1E-05}}

I need to get rid of scientific notation so that resulting JObject would look like original json.我需要摆脱科学记数法,以便生成的 JObject 看起来像原始 json。

I managed to do that using later version of Newtonsoft.Json like that:我设法使用更高版本的 Newtonsoft.Json 做到了这一点:

var serializer = new JsonSerializer { FloatParseHandling = FloatParseHandling.Decimal };
using (System.IO.TextReader tr = new System.IO.StringReader(str)
using (var jsonReader = new JsonTextReader(tr))
{
    var jp = serializer.Deserialize(jsonReader);
    var jObject = JObject.FromObject(jp);
}

But I need to achieve the same result using Newtonsoft.Json version 3.5 which does not have a FloatParseHandling property.但是我需要使用没有 FloatParseHandling 属性的 Newtonsoft.Json 3.5 版来实现相同的结果。 I guess I need to implement a JsonConverter somehow, but I have no idea how to do that, since my real json is much more complex than the one in example and I need to handle all the float values in it the right way.我想我需要以某种方式实现 JsonConverter,但我不知道该怎么做,因为我真正的 json 比示例中的要复杂得多,我需要以正确的方式处理其中的所有浮点值。

So, what would be the right way to get a JObject without a scientific notation for float values using Newtonsoft 3.5?那么,使用 Newtonsoft 3.5 在没有浮点值科学记数法的情况下获取 JObject 的正确方法是什么?

Following produces the object you are looking for以下生成您正在寻找的对象

JObject.Load(new JsonTextReader(new StringReader(str)) { FloatParseHandling = FloatParseHandling.Decimal }, null)

taken from here:取自这里:

EDIT: JTokenTypes in NewtonSoft v 3.5.8 are limited to Float and Integer (in regards to decimal).编辑:NewtonSoft v 3.5.8 中的 JTokenTypes 仅限于 Float 和 Integer(关于十进制)。 There is no decimal type in that version and thus makes it not possilbe to have a decimal value in that JObject.该版本中没有十进制类型,因此在该 JObject 中不可能有十进制值。

JTokenTypes from v3 of newtonsoft newtonsoft v3 中的 JTokenTypes

    None = 0,
    Object = 1,
    Array = 2,
    Constructor = 3,
    Property = 4,
    Comment = 5,
    Integer = 6,
    Float = 7,
    String = 8,
    Boolean = 9,
    Null = 10,
    Undefined = 11,
    Date = 12,
    Raw = 13,
    Bytes = 14

The right way to do this would be to upgrade the Newtonsoft package :)正确的方法是升级 Newtonsoft 包:)

Jawad's provided code is not the best solution because it will end up in memory leaks. Jawad 提供的代码不是最好的解决方案,因为它最终会导致内存泄漏。 StringReader and JsonTextReader are both implementing the IDisposable interface and therefore must be disposed if they are not used anmyore. StringReaderJsonTextReader都实现了IDisposable接口,因此如果不使用它们,则必须对其进行处理。 Safer code would be:更安全的代码是:

public JObject CustomJObjectLoad(string str)
{
    using (var stringReader = new StringReader(str))
    {
        using (var jsonReader = new JsonTextReader(stringReader) { FloatParseHandling = FloatParseHandling.Decimal })
        {
            return JObject.Load(jsonReader, null);
        }
    }
}

First, your json isn't valid :) It should have double quotes:首先,您的 json 无效 :) 它应该有双引号:

{"data": {"someProperty": 0.00001}}

But even better, using standard casing, would be:但更好的是,使用标准外壳,将是:

{"Data": {"SomeProperty": 0.00001}}

And then, we can do this:然后,我们可以这样做:

var str = "{\"Data\": {\"SomeProperty\": 0.00001}}";
dynamic myObject = JsonConvert.DeserializeObject(str);
var val = myObject.Data.SomeProperty.ToString("0." + new string('#', 339));

val will then be: "0.00001" val将是: "0.00001"

(Note: I stole the solution from: Double to string conversion without scientific notation ) (注意:我从以下地址中窃取了解决方案: Double to string conversion without science notation

You can also create simple POCO object.您还可以创建简单的 POCO 对象。 and make sure someProperty is of type string and then de-serialize the json string并确保someProperty是字符串类型,然后反序列化 json 字符串

var myObject = Newtonsoft.Json.JsonConvert.DeserializeObject<YourObject>(json)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM