iOS Question How to get map to group key/value pairs

davepamn

Active Member
Licensed User
Longtime User
I need my json string to format

[{"key":"1","keyvalue":"a"},{"key":"2","keyvalue":"b"}]

B4X:
Server Code:

<Serializable> _
    Public Class cKeyItem
        Private m_key As String
        Private m_keyValue As String
        PublicProperty key() AsString
            Get
                Return m_key
            End Get
            Set(value As String)
                m_key = value
            End Set
        EndProperty
        Public Property keyValue() As String
            Get
                Return m_keyValue
            End Get
            Set(value As String)
                m_keyValue = value
            End Set
        EndProperty
    EndClass

<WebMethod()> _
Sub Save(sJSON as string)
Dim jSearializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()

        Dim objItemList = jSearializer.Deserialize(Of List(Of cKeyItem))(sJSON)
        Dim oKeyItem As cKeyItem
        For Each oKeyItem In objItemList
            sRetVal = sRetVal & oKeyItem.key & "=" & oKeyItem.keyValue
        Next
End Sub

How do I get the map function to serialize the json string into key value pairs?

B4X:
    Dim sJSONString As String
    Dim oJSONGenerator As JSONGenerator
    oJSONGenerator.Initialize2(oKeyValueList)
    sJSONString=oJSONGenerator.ToString
 
Last edited:

davepamn

Active Member
Licensed User
Longtime User
jobTestIdValues.Download2("http://server/Save", Array As String("sJSON",sJSONString))

I tested a large JSON string on the server and it worked.

There seems to be a problem with the sJSONString on the client.

B4X:
Dim oKeyValueList As List
oKeyValueList.Initialize

... (loop)
oKeyValueList.Add(CreateMap("key":sTestId,"keyvalue":sTestValue))  '100 of these tests
...

Dim sJSONString As String
Dim oJSONGenerator As JSONGenerator
oJSONGenerator.Initialize2(oKeyValueList)
sJSONString=oJSONGenerator.ToString

Msgbox(sJSONString,"json")

Dim jobTestIdValues As HttpJob

jobTestIdValues.Initialize("JobSaveTestIdValues",Me)
jobTestIdValues.Download2("http://www.abc.com/Save", ArrayAsString("sJSON",sJSONString))

The msgbox shows the JSON string is being generated correctly.

The code works for 20 tests.
 
Last edited:
Upvote 0

davepamn

Active Member
Licensed User
Longtime User
I wrote a VB app and set a large JSON string to the server and it parsed. The asp.net server works.
 
Upvote 0

davepamn

Active Member
Licensed User
Longtime User
B4X:
Dim jobTestIdValues As HttpJob
jobTestIdValues.Initialize("JobSaveTestIdValues",Me)
jobTestIdValues.PostString("http://www.abc.com/Save", "sJSON=" & sJSONString)

This Worked!
 
Upvote 0
Top