C# JSON将字符串转换为整数关于数字分组符号

C# JSON将字符串转换为整数关于数字分组符号,c#,json,json.net,C#,Json,Json.net,我得到一个错误,“无法将字符串转换为整数:3.500。”。json转换为对象时路径“数量” json: {"ProductCalcKey":"xxx","PaperType":"1","Quantity":"3.500"} 对象: public class UnitPrice { public int UnitPriceId { get; set; } public int QuantityMin { get; set; } public int QuantityMax

我得到一个错误,“无法将字符串转换为整数:3.500。”。json转换为对象时路径“数量”

json:

{"ProductCalcKey":"xxx","PaperType":"1","Quantity":"3.500"}
对象:

public class UnitPrice
{
    public int UnitPriceId { get; set; }
    public int QuantityMin { get; set; }
    public int QuantityMax { get; set; }
    public decimal Price { get; set; }
    public string ProductCalcKey { get; set; }
    public PaperType? PaperType { get; set; }
    public int Quantity { get; set; }
}
我使用以下方法

protected object FromJsonToObject(Type t)
{
    Context.Request.InputStream.Position = 0;
    string json;
    using (var reader = new StreamReader(Context.Request.InputStream))
    {
        json = reader.ReadToEnd();
    }

    // todo: string to integer such as '222.222.222'
    return JsonConvert.DeserializeObject(json, t, new IsoDateTimeConverter());
}

如何在不接触jsontext的情况下解决此问题

我用这种方法解决了这个问题

public class FormatConverter : JsonConverter
{
    public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
    {
        throw new NotImplementedException();
    }

    public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
    {
        if (objectType == typeof(int))
        {
            return Convert.ToInt32(reader.Value.ToString().Replace(".", string.Empty));
        }

        return reader.Value;
    }

    public override bool CanConvert(Type objectType)
    {
        return objectType == typeof(int);
    }
}


[Test]
public void ConvertJson()
{
    const string Json = "{\"ProductCalcKey\":\"xxx\",\"PaperType\":\"1\",\"Quantity\":\"3.500\"}";
    var o = (UnitPrice)JsonConvert.DeserializeObject(Json, typeof(UnitPrice), new FormatConverter());
    Assert.AreEqual(3500, o.Quantity);
}

将数量从int更改为string,并在反序列化后将其转换。我无法执行此操作。这是一般结构。我还将用于其他事情。我无法触摸json文本。我必须用json转换器解决这个问题。我不确定,但我不相信你能做到。我希望有人能给你答案。我,在那种情况下我不知道,但我对答案也很好奇。我会跟着那篇文章走到底。这篇文章也是+1。这里还有另一个我的问题:。我认为可以这样做。问题非常相似。但我做不到。我不知道该怎么做。如果我错了,请纠正我,但你说的帖子是关于格式化字符串字段的,而不是关于转换。你应该使用
Int32.Parse(“3.500”,NumberStyles.allowthands,CultureInfo.GetCultureInfo(“it”)
,而不是操纵字符串(将
“it”
更改为JSON中使用的区域性),它在架构上更为正确。