Answered by:
How to return two objects (including one as list of) in JSON with WCF

Question
-
Hi,
in XML you can do somethign like this:<xml> <persons> <person> <name>a</name> <lastname>b</lastname> </person> <person> <name>c</name> <lastname>d</lastname> </person> <person> <name>e</name> <lastname>f</lastname> </person> </persons> <endresult> <result>122938</result> </endresult> </xml>
Is this in JSON possible, and if yes, how can i do this in my WCF Service?
Best regards,
Yavuz B.
My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog List- Edited by [Yavuz B.] Sunday, March 23, 2014 10:05 AM
Wednesday, March 19, 2014 4:53 PM
Answers
-
Hi,
Please first do not forget the add the [DataMember] to the two classes, then please modify your datacontract as following:
<DataContract> _ Public Class Book <DataMember> _ Public Property bookchapter() As List(Of Chapter) Get Return m_bookchapter End Get Set m_bookchapter = value End Set End Property Private m_bookchapter As List(Of Chapter) <DataMember> _ Public Property success() As Integer Get Return m_success End Get Set m_success = value End Set End Property Private m_success As Integer Public Sub New() bookchapter = New List(Of Chapter)() End Sub End Class <DataContract> _ Public Class Chapter <DataMember> _ Public Property description() As String Get Return m_description End Get Set m_description = value End Set End Property Private m_description As String End Class
Then it should work, the result is as following:
Best Regards,
Amy Peng
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- Edited by Amy PengMicrosoft employee Tuesday, March 25, 2014 10:32 AM
- Marked as answer by [Yavuz B.] Tuesday, March 25, 2014 11:46 AM
Tuesday, March 25, 2014 10:27 AM -
Hi,
Because you need to initialization your list in the contructor.
Thanks.
- Marked as answer by [Yavuz B.] Wednesday, March 26, 2014 11:26 AM
Wednesday, March 26, 2014 2:21 AM
All replies
-
Hi,
The DataContractJsonSerializer is intended for use with WCF client applications where the serialized types are typically POCO classes with the DataContract attribute applied to them. No DataContract, no serialization. The mapping mechanism of WCF makes the sending and receiving very simple, but only if your platform is homogeneous. If you start mixing in different toolsets, your program might go sideways. So in your case, it will hard to use the DataContractJsonSerializer. Then I use another way, please try to check it:
The example:
string xml = "<xml><persons><person><name>a</name><lastname>b</lastname></person><person><name>c</name><lastname>d</lastname></person><person><name>e</name><lastname>f</lastname></person></persons><endresult><result>122938</result></endresult></xml>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string json = JsonConvert.SerializeXmlNode(doc); Console.WriteLine(json); Console.ReadLine();
Then the result:
Best Regards,
Amy Peng
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.- Marked as answer by [Yavuz B.] Thursday, March 20, 2014 8:33 AM
- Unmarked as answer by [Yavuz B.] Friday, March 21, 2014 10:27 AM
Thursday, March 20, 2014 7:12 AM -
Does this mean, that it is not possible to such a class in the DataContract part and i have to response my JSON Data as string?
Best regards,
Yavuz B.
My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog ListThursday, March 20, 2014 9:21 AM -
Thanks for your replies and sorry, i dont want to be rude. But i dont understand it. I have tried the following:
IService
<OperationContract()> _ <WebInvoke(Method:="GET", BodyStyle:=WebMessageBodyStyle.Wrapped, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="/GetKinder")> _ Function GetKinder() As List(Of MeineKlasse) <DataContract()> _ Class MeineKlasse Private m_firstname As String Private m_kind As List(Of Kind) <DataMember()> _ Public Property firstname As String Get Return m_firstname End Get Set(value As String) m_firstname = value End Set End Property <DataMember()> _ Public Property kinder As List(Of Kind) Get Return m_kind End Get Set(value As List(Of Kind)) m_kind = value End Set End Property End Class <DataContract()> _ Class Kind Private m_kind_name As String <DataMember()> _ Public Property kind_name As String Get Return m_kind_name End Get Set(value As String) m_kind_name = value End Set End Property End Class
Service.vb
Public Function GetKinder() As List(Of IService1.MeineKlasse) Implements IService1.GetKinder Dim meineKlasse As New List(Of IService1.MeineKlasse) For i As Integer = 0 To 9 Dim kind As New IService1.MeineKlasse kind.firstname = "meinname" & i.ToString Dim mk As IService1.Kind = New IService1.Kind() mk.kind_name = "adjasd" kind.kinder.Add(mk) meineKlasse.Add(kind) Next Return meineKlasse End Function
When i execute my service i get and error "The server encountered an error processing the request. See server logs for more details." But i cant find anything in the eventlog.
When i comment out
Dim mk As IService1.Kind = New IService1.Kind() mk.kind_name = "adjasd" kind.kinder.Add(mk)
it works! But i comment in it doesnt work. Curious!?
Best regards,
Yavuz B.
My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog List- Edited by [Yavuz B.] Thursday, March 20, 2014 5:21 PM
Thursday, March 20, 2014 12:45 PM -
I cannot unmark as answer the post above, which i markes as answer! Is this a bug?
Best regards,
Yavuz B.
My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog ListFriday, March 21, 2014 10:31 AM -
Hm, unmark has seems to be working now, but i am not able to solve my problem :(
Best regards,
Yavuz B.
My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog ListFriday, March 21, 2014 7:33 PM -
I have tried now the following:
My ServiceContract
<OperationContract()> _ <WebInvoke(Method:="GET", BodyStyle:=WebMessageBodyStyle.Wrapped, ResponseFormat:=WebMessageFormat.Json, UriTemplate:="/GetBooks")> _ Function GetBooks() As List(Of Book) <DataContract()> _ Class Book Public Property bookchapter() As List(Of Chapter) Get Return m_bookchapter End Get Set(value As List(Of Chapter)) m_bookchapter = value End Set End Property Private m_bookchapter As List(Of Chapter) Public Property success() As Integer Get Return m_success End Get Set(value As Integer) m_success = value End Set End Property Private m_success As Integer End Class <DataContract()> _ Class Chapter Public Property description() As String Get Return m_description End Get Set(value As String) m_description = value End Set End Property Private m_description As String End Class
My Service
Public Function GetBooks() As List(Of IGPService.Book) Implements IGPService.GetBooks Dim books As New List(Of IGPService.Book) Dim newBook As IGPService.Book = New IGPService.Book For i As Integer = 0 To 3 Dim newchapter As IGPService.Chapter = New IGPService.Chapter newchapter.description = "Chapter1" newBook.bookchapter.Add(newchapter) Next newBook.success = 1 books.Add(newBook) Return books End Function
But here i get the same error:
"The server encountered an error processing the request. See server logs for more details."
I don't know what to try else ...
Best regards,
Yavuz B.
My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog ListFriday, March 21, 2014 8:27 PM -
Has noone an idea?
Best regards,
Yavuz B.
My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog ListTuesday, March 25, 2014 7:44 AM -
Hi,
Please first do not forget the add the [DataMember] to the two classes, then please modify your datacontract as following:
<DataContract> _ Public Class Book <DataMember> _ Public Property bookchapter() As List(Of Chapter) Get Return m_bookchapter End Get Set m_bookchapter = value End Set End Property Private m_bookchapter As List(Of Chapter) <DataMember> _ Public Property success() As Integer Get Return m_success End Get Set m_success = value End Set End Property Private m_success As Integer Public Sub New() bookchapter = New List(Of Chapter)() End Sub End Class <DataContract> _ Public Class Chapter <DataMember> _ Public Property description() As String Get Return m_description End Get Set m_description = value End Set End Property Private m_description As String End Class
Then it should work, the result is as following:
Best Regards,
Amy Peng
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click HERE to participate the survey.
- Edited by Amy PengMicrosoft employee Tuesday, March 25, 2014 10:32 AM
- Marked as answer by [Yavuz B.] Tuesday, March 25, 2014 11:46 AM
Tuesday, March 25, 2014 10:27 AM -
Thank you very much!
Best regards,
Yavuz B.
My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog ListTuesday, March 25, 2014 11:48 AM -
It works, great!
But, what is the difference between my code an the adding of the New() Contructor?
Best regards,
Yavuz B.
My Sharepoint and Enterprise 2.0 Blog http://www.bogazci.com | Microsoft Sharepoint Resources & Blog ListTuesday, March 25, 2014 5:20 PM -
Hi,
Because you need to initialization your list in the contructor.
Thanks.
- Marked as answer by [Yavuz B.] Wednesday, March 26, 2014 11:26 AM
Wednesday, March 26, 2014 2:21 AM -
datalogic
class 1.vb
Public Class Class1
Public Function GetMachineList() As List(Of WcfService1.Machine) class is not recognizing , but other functions are working fine
Dim oConn As New Informix("xxxxx;")
Dim machines As New List(Of Machine)()
Dim strSql As String = "SELECT k_koneno AS ma_machineid,k_paikka AS ma_placenumber,k_malli AS ma_model,k_tyyppi AS ma_type,k_versio AS ma_version FROM kone"
Try
oConn.OpenDB()
Dim reader = oConn.SelectQueryIntoReader(strSql)
If reader IsNot Nothing Then
While reader.Read()
Dim machine As New Machine()
machine.ma_machineid = reader.GetString(0)
machine.ma_placenumber = reader.GetString(1)
machine.ma_model = reader.GetString(2)
machine.ma_type = reader.GetString(3)
machine.ma_version = reader.GetString(4)
machines.Add(machine)
End While
End If
Return machines
Catch ex As Exception
Throw New System.Exception(ex.Message)
Finally
oConn.CloseDB()
End Try
End Function
Public Function GetLastName() As String
Dim oConn As New Informix("xxxxx")
Dim oo As String
Try
oConn.OpenDB()
oo = oConn.GetFirstValue("select p_fornamn from person where p_efternamn = 'JATOI'").ToString
Return oo.ToString
Catch ex As Exception
Throw New System.Exception(ex.Message)
Finally
oConn.CloseDB()
End Try
End Function
Public Function GetPersonsTable() As DataSet
Dim oConn As New Informix("xxx")
Dim daTable As New DataTable
Dim dsTable As New DataSet
Try
oConn.OpenDB()
Dim person = oConn.SelectQueryIntoTable("SELECT k_koneno AS ma_machineid, k_paikka AS ma_placenumber, k_malli AS ma_model,k_tyyppi AS ma_type, k_versio AS ma_version FROM kone WHERE k_paikka = 18 AND k_autmod = 'A'", daTable)
dsTable.Tables.Add(daTable)
Return dsTable
Catch ex As Exception
Throw New System.Exception(ex.Message)
Finally
oConn.CloseDB()
End Try
End Function
Public Function GetPersonsTest() As DataSet
Dim oConn As New Informix("xxxxx")
Dim daTable As New DataTable
Dim dsTable As New DataSet
Try
oConn.OpenDB()
Dim person = oConn.SelectQueryIntoTable("SELECT * from person", daTable)
dsTable.Tables.Add(daTable)
Return dsTable
Catch ex As Exception
Throw New System.Exception(ex.Message)
Finally
oConn.CloseDB()
End Try
End Function
End Classwcf service1
Iservice1.Vb
Imports System.Web.Script.Services
' NOTE: You can use the "Rename" command on the context menu to change the interface name "IService1" in both code and config file together.
<ServiceContract()>
Public Interface IService1
<OperationContract()>
<WebInvoke(Method:="Get",
ResponseFormat:=WebMessageFormat.Json,
BodyStyle:=WebMessageBodyStyle.Wrapped,
UriTemplate:="GetDataJSON")> _
Function GetData(ByVal value As Integer) As String
<OperationContract()>
Function GetPersonTable() As DataSet
<OperationContract()>
Function GetPersonTest() As DataSet
<OperationContract()>
Function GetMachineList() As List(Of Machine)
<OperationContract()>
Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType
' TODO: Add your service operations here
End Interface
' Use a data contract as illustrated in the sample below to add composite types to service operations.
<DataContract()>
Public Class CompositeType
<DataMember()>
Public Property BoolValue() As Boolean
<DataMember()>
Public Property StringValue() As String
End Class
<DataContract()>
Public Class Machine
Public Property MachineId() As String
Get
Return mma_machineid
End Get
Set(ByVal value As String)
mma_machineid = value
End Set
End Property
Private mma_machineid As String
Public Property MachinePlaceNumber() As String
Get
Return mma_placenumber
End Get
Set(ByVal value As String)
mma_placenumber = value
End Set
End Property
Private mma_placenumber As String
Public Property MachineModel() As String
Get
Return mma_model
End Get
Set(ByVal value As String)
mma_model = value
End Set
End Property
Private mma_model As String
Public Property MachineType() As String
Get
Return mma_type
End Get
Set(ByVal value As String)
mma_type = value
End Set
End Property
Private mma_type As String
Public Property MachineVersion() As String
Get
Return mma_version
End Get
Set(ByVal value As String)
mma_version = value
End Set
End Property
Private mma_version As String
End ClassService1.Vb
Public Class Service1
Implements IService1
Public Sub New()
End Sub
Public Function GetData(ByVal value As Integer) As String Implements IService1.GetData
Dim oDL As New Datalogic.Class1
Return oDL.GetLastName
End Function
Public Function GetPersonTable() As DataSet Implements IService1.GetPersonTable
Dim oDL As New Datalogic.Class1
Return oDL.GetPersonsTable
End Function
Public Function GetPersonTest() As DataSet Implements IService1.GetPersonTest
Dim oDL As New Datalogic.Class1
Return oDL.GetPersonsTest
End Function
Public Function GetMachineList() As List(Of Machine) Implements IService1.GetMachineList
Dim oDL As New Datalogic.Class1
Return oDL.GetMachineList
End Function
Public Function GetDataUsingDataContract(ByVal composite As CompositeType) As CompositeType Implements IService1.GetDataUsingDataContract
If composite Is Nothing Then
Throw New ArgumentNullException("composite")
End If
If composite.BoolValue Then
composite.StringValue &= "Suffix"
End If
Return composite
End Function
End ClassALL OTHER FUNCIONS ARE WORKING FINE BUT ONLY THE Public Function GetMachineList() As List(Of WcfService1.Machine) class SHOWING ERROR NOW PLEASE HELP i spent days on it.
and i have made reference other functions are working fine but machine list is not working fine
Wednesday, August 6, 2014 7:47 AM