1

I have below JSON object

myvalues : {
     0 : "value0",
     1 : "value1",
     2 : "value2",
     3 : "value3"
}

I want to bind this JSON object into a vb.net class object as an input to a WCF OperationContract method - but I can't define the numeric property names as numbers. I am getting the error message:

Identifier expected on property names because property name cannot be a number

Using the following:

Public class myvalues_class

    public property 0 as string
    public property 1 as string
    public property 2 as string
    public property 3 as string

end class

how can I convert this JSON object to a vb.net object class?

dbc
  • 104,963
  • 20
  • 228
  • 340
Metin Genç
  • 99
  • 2
  • 13
  • 3
    Looks more like a dictionary than a list of properties. If you really want properties, you can take a look at DataContract – the_lotus May 23 '19 at 15:05
  • 1
    You can deserialize `myvalues` into a `Dictionary(Of Integer, String)` or add `` attributes to the properties in `myvalues_class` as in [How can I parse a JSON string that would cause illegal C# identifiers?](https://stackoverflow.com/a/24536564/3744182). That question is for c# but the same answers apply. Agree it's a duplicate? – dbc May 24 '19 at 03:46
  • @dbc thanks for answer. I tried to add ` _` above of property and error message gone. But cannot deserialize JSON property 0 into VB.NET class property 0. Yes unfortunatelly a duplicate. Sorry – Metin Genç May 24 '19 at 06:57
  • 1
    Glad to help & no need to apologize - your question is clear and has no vb.net duplicate. *But cannot deserialize JSON property 0 into VB.NET class property 0.* - so do you still have a problem? – dbc May 24 '19 at 07:03
  • @dbc yes unfortunately i still have a problem. Cannot deserialize json into a vb.net object. I also tried ` _` but doesn't changed anything. cannot deserialize json object. Canyou check [link]https://pastebin.com/RDYuSs2W please – Metin Genç May 24 '19 at 07:23
  • @dbc and the other members being converted without any problem except "values" member. firstName, lastname has no problem. Problem is only on "values", always converting r,g,b,a values as 0 (because defined as byte) – Metin Genç May 24 '19 at 07:27
  • 1
    Seems like it works perfectly, see https://dotnetfiddle.net/fvR9ED – dbc May 24 '19 at 07:36
  • 1
    Or maybe you're not using [tag:json.net]? What serializer are you using? Can you share a [mcve]? – dbc May 24 '19 at 07:41
  • @dbc thanks for answer.This is WCF service which has getting JSON object from client as POST. I using Javascript to post JSON object into WCF service. Please check this Link [link]https://pastebin.com/QDjkBiVm so i don need to use `JsonConvert.DeserializeObject ` code for convert. because WCF service doing this. – Metin Genç May 24 '19 at 07:59
  • 1
    Well WCF uses `DataContractJsonSerializer` so you need to use data contract attributes.... – dbc May 24 '19 at 08:05
  • 1
    @dbc oh man :) thanks It's worked like charm. I tried ` _` before you say, but i had never thought about using ` _` before. Really thanks. – Metin Genç May 24 '19 at 08:12

1 Answers1

1

uses DataContractJsonSerializer, so you will need to annotate your Values type with data contract attributes that map some validly-named vb.net properties to the JSON numeric property names, e.g. like so:

<System.Runtime.Serialization.DataContract> _
Public Class Values
    <DataMember(Name:="0")> _
    Public Property r As Integer

    <DataMember(Name:="1")> _
    Public Property g As Integer

     <DataMember(Name:="2")> _
    Public Property b As Integer

    <DataMember(Name:="3")> _
    Public Property a As Integer
End Class

Note that the data contract serializers are opt-in so you will have to mark all properties to be serialized with DataMemberAttribute.

dbc
  • 104,963
  • 20
  • 228
  • 340