代码之家  ›  专栏  ›  技术社区  ›  Gforse

JSON转换为double并绑定到wpf numericaupdown失败

  •  0
  • Gforse  · 技术社区  · 6 年前

    当前,绑定出错。情况是我将设置写入一个JSON文件。当应用程序再次打开时,JSON文件将在整个应用程序中被读取和使用。现在有一件奇怪的事情:当我将一个双精度值绑定到numericaupdown的值时,我会得到一个错误: type 'JValue' to type 'System.Nullable1[System.Double]'for 'en-US' 但是,当我重新创建JSON列表和文件时,不会发生此错误。(简单地说,当我删除文件并重新启动应用程序时,它将创建一个类的新实例,并将其写入磁盘)

    类中的属性:

    Public Property SomeValue As Double
    

    写/读JSON:

    'Write
    Using _file As StreamWriter = New StreamWriter(SettingFilePath)
                Dim serializer As New JsonSerializer()
                serializer.Formatting = Formatting.Indented
                serializer.Serialize(_file, Me)
    End Using
    
    'Read
    Return JsonConvert.DeserializeObject(Of Settings)(File.ReadAllText(settingsfile))
    

    JSON字符串:

    "SomeValue": 1.0,
    

    XAML中的绑定:

    <Controls:NumericUpDown
                    Width="200"
                    HorizontalAlignment="Center"
                    Maximum="5"
                    Minimum="1"
                    NumericInputMode="All"
                    Speedup="false"
                    Value="{Binding SomeValue}" />
    

    请注意,我使用的是MathApps Metro NumericUpDown控制版本1.6.5

    NewtonSoft版本10.0.0.1(由于依赖关系而无法更新)

    编辑:

    当被问到时,我更深入地挖掘,现在知道它从哪里开始,但还不知道如何解决它。从我的课开始,例如:

    Public class Hello    
       Dim a as Object
       Dim b as EnumTypeOfObjectIn_A
       Dim SomeOtherStuff as String    
    End class
    

    现在当我 DeserializeObject 将文件解压缩到类 Hello THEN变量 a 成为类型的对象 JObject 这就是为什么许多逻辑后记出错的原因。当我用代码创建对象时,一切都很顺利,因为 TypeOf 对象与我放入的对象匹配。有没有为 Deserializer 将对象转换为变量中指示的对象 b ?

    1 回复  |  直到 6 年前
        1
  •  0
  •   Gforse    6 年前

    找到了我要找的解决方案。这个 Newtonsoft JSON 包含 JsonSerializerSettings 帮助反序列化进程的类。对我来说,在序列化时添加对象类型很重要,因此 TypeNameHandling TypeNameAssemblyFormatHandling 是为了在 牛顿福特JSON 装配

    最后我得到了这个代码:

    Public Class Hello
    
        Public Property A As Object
        Dim settingsfile As String = "C:\jsontest.json"
    
        Public Sub Save()
    
            Using _file As StreamWriter = New StreamWriter(settingsfile)
                _file.Write(JsonConvert.SerializeObject(Me, Formatting.Indented, New JsonSerializerSettings() With {
                                                    .TypeNameHandling = TypeNameHandling.Objects,
                                                    .TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Simple
                                                    }))
            End Using
        End Sub
    
        Public Function Load() As Hello
            Return JsonConvert.DeserializeObject(Of Hello)(File.ReadAllText(settingsfile), New JsonSerializerSettings() With {.TypeNameHandling = TypeNameHandling.Objects})
        End Function
    End Class
    
    Public Class Person
        Public Property Name As String
        Public Property Age As Integer
    
        Sub New()
            Me.Name = "John"
            Me.Age = 130
        End Sub
    End Class
    

    生成这个JSON输出,注意 $Type

    {
      "$type": "MyNamespace.Hello, MyNamespace",
      "A": {
        "$type": "MyNamespace.Person, MyNamespace",
        "Name": "John",
        "Age": 130
      }
    }