Answered by:
JSON Data contract and hypens

Question
-
Hi
I want to receive a JSON object through WCF - I have set up a data contract and it works fine. The problem I have now hit is that the object being sent has some parameters with hyphens (e.g. q-125). As I can't create a property with a hyphen in its name, I'm not sure how I can map these parts of the JSON object to my data contract.
For example, if the JSON object is {"surname":"Pitt","forename":"Mike","q-125":"No"} I set my data contract like this:-
<DataContract()>
Public Class theData
<DataMember()>
Property forename As String = ""
<DataMember()>
Property surname As String = ""
End Class
This works fine with my web service and pulls in forename and surname. How would I go about receiving q-125 as I can't add:-
Property q-125 as string = ""
Thanks in advance,
Mike.
Tuesday, November 29, 2011 11:11 AM
Answers
-
You can use the Name property of the <DataMember> attribute to configure the name of the JSON object member. The code below shows how the JSON you mentioned can be received by a WCF service.
Public Class Post_9d1a0c6e_8e83_420a_8bd3_7a13cc8eadb4 <DataContract()> _ Public Class TheData <DataMember()> _ Property forename As String <DataMember()> _ Property surname As String <DataMember(Name:="q-125")> _ Property Q125 As String End Class <ServiceContract()> _ Public Interface ITest <WebInvoke()> _ Function Process(ByVal data As TheData) As String End Interface Public Class Service Implements ITest Public Function Process(ByVal data As TheData) As String Implements ITest.Process Return String.Format("TheData[fore={0},sur={1},q-125={2}]", data.forename, data.surname, data.Q125) End Function End Class Public Shared Sub Test() Dim baseAddress As String = "http://" + Environment.MachineName + ":8000/Service" Dim host As WebServiceHost = New WebServiceHost(GetType(Service), New Uri(baseAddress)) host.Open() Console.WriteLine("Host opened") Dim client As WebClient = New WebClient() client.Headers(HttpRequestHeader.ContentType) = "application/json" Dim json As String = "{""surname"":""Pitt"",""forename"":""Mike"",""q-125"":""No""}" Console.WriteLine(client.UploadString(baseAddress + "/Process", json)) host.Close() End Sub End Class
Carlos Figueira- Proposed as answer by CarlosFigueiraMicrosoft employee Thursday, December 1, 2011 7:05 PM
- Marked as answer by Yi-Lun Luo Monday, December 5, 2011 8:49 AM
Wednesday, November 30, 2011 6:50 AM
All replies
-
Hello, this is not supported. q-125 may be a valid string variable and thus can be used in JSON, it can't be used in programming language (including JavaScript) as variable names. You should change it to q_125.
If you have to use q-125, you cannot use DataContractJsonSerializer. You have to write a custom serializer. Also note even if you've made it to work on the WCF side, it may not work on the client. For example, if you need a JavaScript client, you won't be able to use Eval to turn the response into a JavaScript object.
Lante, shanaolanxing This posting is provided "AS IS" with no warranties, and confers no rights.
If you have feedback about forum business, please contact msdnmg@microsoft.com. But please do not ask technical questions in the email.Wednesday, November 30, 2011 2:16 AM -
You can use the Name property of the <DataMember> attribute to configure the name of the JSON object member. The code below shows how the JSON you mentioned can be received by a WCF service.
Public Class Post_9d1a0c6e_8e83_420a_8bd3_7a13cc8eadb4 <DataContract()> _ Public Class TheData <DataMember()> _ Property forename As String <DataMember()> _ Property surname As String <DataMember(Name:="q-125")> _ Property Q125 As String End Class <ServiceContract()> _ Public Interface ITest <WebInvoke()> _ Function Process(ByVal data As TheData) As String End Interface Public Class Service Implements ITest Public Function Process(ByVal data As TheData) As String Implements ITest.Process Return String.Format("TheData[fore={0},sur={1},q-125={2}]", data.forename, data.surname, data.Q125) End Function End Class Public Shared Sub Test() Dim baseAddress As String = "http://" + Environment.MachineName + ":8000/Service" Dim host As WebServiceHost = New WebServiceHost(GetType(Service), New Uri(baseAddress)) host.Open() Console.WriteLine("Host opened") Dim client As WebClient = New WebClient() client.Headers(HttpRequestHeader.ContentType) = "application/json" Dim json As String = "{""surname"":""Pitt"",""forename"":""Mike"",""q-125"":""No""}" Console.WriteLine(client.UploadString(baseAddress + "/Process", json)) host.Close() End Sub End Class
Carlos Figueira- Proposed as answer by CarlosFigueiraMicrosoft employee Thursday, December 1, 2011 7:05 PM
- Marked as answer by Yi-Lun Luo Monday, December 5, 2011 8:49 AM
Wednesday, November 30, 2011 6:50 AM -
Yi-Lun, in JS you can have objects with keys which aren't valid property names; you just need to declare them as strings:var myObj = {forename: "Mike", surname: "Pitt", "q-125": "No"};
And the DataContractJsonSerializer does support it, you need to configure the name using the Name property in the DataMemberAttribute.
Carlos FigueiraWednesday, November 30, 2011 6:54 AM