Click here to Skip to main content
15,884,298 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
hi all,

how do i create :

{"data":[{"nopol":"B1106PFB"},{"nopol":"B1120UOK"},{"nopol":"B6082PPR"},{"nopol":"B6354PDW"},{"nopol":"B777"},{"nopol":"BG8882KP"}]}

using json_encode on asp.net (vb)

thanks
Posted
Updated 17-Aug-14 22:03pm
v2
Comments
Sergey Alexandrovich Kryukov 18-Aug-14 3:37am    
PHP json_encode? Javascrip json_encode (where did you get it then and why?)?
How is that related to ASP.NET?
What have you tried?
-SA
JasonMacD 18-Aug-14 9:58am    
Try one of the online formatters to test your json http://www.jsoneditoronline.org/

Create a class the mirrors your Json data.
VB
<datacontract> _
Public Class Datum
     <datamember> _
     Public Property nopol As String
End Class

<datacontract> _
Public Class CP
     <datamember> _
     Public Property data As Datum()
End Class


Load the objects with data.
VB
Dim oDatum As New Datum() With {.nopol = "Test Values"}
Dim oData As New CP() With {.data = {oDatum}}


Then call the Json Serializer
VB
Using oMemStream As New IO.MemoryStream()
    Dim oJsonSerializer As New Json.DataContractJsonSerializer(GetType(oData))
    oJsonSerializer.WriteObject(oMemStream, oData)
    Dim szJsonString As String = System.Text.Encoding.ASCII.GetString(oMemStream.ToArray())
End Using
 
Share this answer
 
v2
hi all,

after searching and trying with the best method that i understand
finally i decided using newton json

here it is :

Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

Dim counter As Integer = 0
Dim jsonstr As String = Request.Form("field1").ToString
Dim ser As JObject = JObject.Parse(jsonstr)
Dim data1 As List(Of JToken) = ser.Children().ToList
Dim output As String = ""
Dim field As String = "("
Dim valstr As String = ""

For Each item As JProperty In data1
item.CreateReader()
For Each comment As JObject In item.Values
Dim u As String
Dim d As String = comment("fuel")
Dim c As String = comment("nopolisi")
Dim su As String = comment("surveyId")
Dim km As String = comment("km")
Dim kondisi As String = comment("kondisi")
Dim keterangan As String = comment("keterangan")
nopol = c
If comment("image_data").Count = 0 Then
u = comment("image_data")
output += "('" + u + "','" + d + "','" + c + "','" + su + "','" + km + "','" + kondisi + "','" + keterangan + "'),"
Else
For i = 0 To comment("image_data").Count - 1
u = comment("image_data")(i)
output += "('" + u + "','" + d + "','" + c + "','" + su + "','" + km + "','" + kondisi + "','" + keterangan + "'),"
Next
End If
Next
Next

that's it.

hope it can helps others, who's facing problems like me :)

thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900