反序列化GSON生成的JSON字符串n C#

时间:2016-12-21 07:56:16

标签: c# json gson

我使用GSON通过java生成了JSON。我想在C#中重新创建相同的字段。 JSON看起来像这样。

{
    "bool1": true,
    "long1": 0,
    "double1": 500.0,
    "int1": 1,
    "double2": 0.4,
    "double3": 0.3,
    "double4": 1.0,
    "double5": 0.1,
    "int2": 0,
    "int3": 0,
    "long2": 20160901,
    "class1object1": {
        "serialVersionUID": 1,
        "class1int1": 0,
        "class1int2": 0,
        "class1int3": 0,
        "class1double1": 68.3,
        "class1doubleArray1": [68.3],
        "class1double2": 65.45,
        "class1doubleArray2": [65.45],
        "class1int4": 1
    },
    "class1object2": {
        "serialVersionUID": 1,
        "class1int1": 135,
        "class1int2": 138,
        "class1int3": 105,
        "class1double1": 68.1,
        "class1doubleArray1": [67.57, 67.6, 67.48, 67.45, 67.39, 67.42, 67.38, 67.37, 67.35, 67.35, 67.37, 67.35, 67.36, 67.37, 67.35, 67.37, 67.38],
        "class1double2": 67.26,
        "class1doubleArray2": [67.5, 67.53, 67.46, 67.4, 67.39, 67.38, 67.37, 67.32, 67.33, 67.33, 67.33, 67.35, 67.36, 67.33, 67.33, 67.37, 67.34],
        "class1int4": 240
    },
    "int4": 1,
    "class2object1": {
        "serialVersionUID": 1,
        "class2double1": 2.8499999999999943,
        "class2double2": 1.6758422688703467,
        "class2double3": 0.09523809523809523
    },
}

我的C#应用​​程序中已经存在所有这些类和其他字段的定义。如果需要,字段的名称也可以保持与JSON中的相同。

有人可以帮助我使用什么工具来实现这个目标以及如何实现?此外,还有可能存在NaN / Inf值(次要问题,不是很重要,如果需要,可以从JSON中省略。

1 个答案:

答案 0 :(得分:1)

您可以使用JSON2CSHARP或将json复制到剪贴板>转到Visual Studio>编辑>选择性粘贴>将JSON粘贴为类。它给你模型课。你可以反序列化json的Uisng Newtonsoft.Json包:

JsonConvert.DeserializeObject<RootClassName>("json");

您的模型类应该看起来:

 public class Class1
{
    public int serialVersionUID { get; set; }
    public int class1int1 { get; set; }
    public int class1int2 { get; set; }
    public int class1int3 { get; set; }
    public double class1double1 { get; set; }
    public List<double> class1doubleArray1 { get; set; }
    public double class1double2 { get; set; }
    public List<double> class1doubleArray2 { get; set; }
    public int class1int4 { get; set; }
}

public class Class2
{
    public int serialVersionUID { get; set; }
    public int class1int1 { get; set; }
    public int class1int2 { get; set; }
    public int class1int3 { get; set; }
    public double class1double1 { get; set; }
    public List<double> class1doubleArray1 { get; set; }
    public double class1double2 { get; set; }
    public List<double> class1doubleArray2 { get; set; }
    public int class1int4 { get; set; }
}

public class Class3
{
    public int serialVersionUID { get; set; }
    public double class2double1 { get; set; }
    public double class2double2 { get; set; }
    public double class2double3 { get; set; }
}

public class RootObject
{
    public bool bool1 { get; set; }
    public int long1 { get; set; }
    public double double1 { get; set; }
    public int int1 { get; set; }
    public double double2 { get; set; }
    public double double3 { get; set; }
    public double double4 { get; set; }
    public double double5 { get; set; }
    public int int2 { get; set; }
    public int int3 { get; set; }
    public int long2 { get; set; }
    public Class1 Class1 { get; set; }
    public Class2 Class2 { get; set; }
    public int int4 { get; set; }
    public Class3 Class3 { get; set; }
}

此外,如果您想在C#类中使用其他名称,则需要使用JsonProperty(PropertyName = "yourJsonName"),这允许您指定此示例应该有用的其他名称:

public class Foo
{
    [JsonProperty(PropertyName = "fooJSONname")]
    public int FooNumber { get; set; }

}