1 つのオブジェクトの JSON 配列を返す次のコードがあります。
Imports System.ServiceModel
Imports System.ServiceModel.Activation
Imports System.ServiceModel.Web
Imports System.IO
Imports System.Runtime.Serialization.Json
Imports System.Runtime.Serialization
<ServiceContract(Namespace:="")> _
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)> _
Public Class MessageService
<OperationContract()> _
Public Function GetMessage(Id As Integer) As String
Dim m As New NewMessage()
m.Id = 1
m.Text = "Text test"
Dim stream As New MemoryStream()
Dim serializer As New DataContractJsonSerializer(GetType(NewMessage))
serializer.WriteObject(stream, m)
serializer.WriteObject(stream, m)
stream.Position = 0
Dim streamReader As New StreamReader(stream)
Return streamReader.ReadToEnd()
End Function
End Class
<DataContract()> _
Public Class NewMessage
<DataMember()> _
Public Id As Integer
<DataMember()> _
Public Text As String
End Class
返されるデータは次のとおりです。
{id:1,Text:"TText test"}
返す方法:
[{id:1,Text:"Text 1"},{id:2,Text:"Text 2"}]