Android Question Json format to the list object

kisoft

Well-Known Member
Licensed User
Longtime User
Hi
I have a list object built with custom types
B4X:
Type modultemp(it As String, nm As String,ad As String, pt As Int, n1 As String, n2 As String, _
                   n3 As String,n4 As String, id As Int, ob As Int,ik As String, o1 As Int, o2 As Int, o3 As Int, o4 As Int, Mw As Int)

The list1 object is converted to Json format, after conversion I get such a Json string
B4X:
["[IsInitialized=true, Mw=0, ad=192.168.8.100\n, id=0, ik=obr2a.png, it=tempmulti.\n, n1=T1, n2=T2, n3=T3\n, n4=T4, nm=Moduł Temp., o1=1\n, o2=2, o3=3, o4=4\n, ob=1, pt=12345]","[IsInitialized=true, Mw=0, ad=192.168.1.100\n, id=2, ik=wloncznik.png, it=strprzekaznik.\n, n1=T1, n2=T2, n3=T3\n, n4=T4, nm=Moduł Przekaźnik, o1=1\n, o2=2, o3=3, o4=4\n, ob=1, pt=1024]","[IsInitialized=true, Mw=0, ad=192.168.1.100\n, id=3, ik=temciswilg2.png, it=cisni_temp_wilgot.\n, n1=T1, n2=T2, n3=T3\n, n4=T4, nm=Moduł Ciś.Temp.Wilg., o1=1\n, o2=2, o3=3, o4=4\n, ob=1, pt=1024]","[IsInitialized=true, Mw=0, ad=192.168.1.100\n, id=4, ik=temciswilg2.png, it=cisni_temp_wilgot.\n, n1=T1, n2=T2, n3=T3\n, n4=T4, nm=Moduł Ciś.Temp.Wilg., o1=1\n, o2=2, o3=3, o4=4\n, ob=1, pt=1024]","[IsInitialized=true, Mw=0, ad=192.168.8.100\n, id=5, ik=obr2a.png, it=tempmulti.\n, n1=T1, n2=T2, n3=T3\n, n4=T4, nm=Moduł Temp., o1=1\n, o2=2, o3=3, o4=4\n, ob=1, pt=12345]"]

I'm sending this string to a second phone using a QR code reader and want to rebuild the list object. I am using this tool and I am getting this code:
B4X:
Dim parser As JSONParser
parser.Initialize(my.json)
Dim root As List = parser.NextArray
For Each colroot As String In root
Next

Now how to properly rebuild a list object? Any tips, suggestions, I've been struggling with it for 4 hours ...
 
Last edited:

aeric

Expert
Licensed User
Longtime User
I normally create a Map then add to a List. From the list convert to Json.
 
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
The list1 object is created by the user and written to the file in this way:
B4X:
'list1 user-created object
Dim raf As RandomAccessFile
        raf.Initialize2(File.DirInternal, "plik1.dat", False, False)
        raf.WriteObject(lista1, False, 0)
        raf.Close
This is how I read the file from list1 and generate the QR code:
B4X:
Dim raf As RandomAccessFile
        raf.Initialize(File.DirInternal, "plik1.dat", False)
        lista1a = raf.ReadObject(raf.CurrentPosition)
        raf.Close
        Dim json As JSONGenerator
        json.Initialize2(lista1a)
        qr.Initialize(ImageView2.Width
        ImageView2.SetBitmap(qr.Create(json.ToString))
Now I read the generated QR code and save it to the txt file:
B4X:
If camEx.IsInitialized Then
    Label2.Text="Gotowe"
    camEx.Release
    If File.Exists(File.DirInternal,"pliktekst.txt") Then
    File.Delete(File.DirInternal,"pliktekst.txt")
    End If
    File.WriteString(File.DirInternal,"pliktekst.txt",raw)
    Activity.Finish
    StartActivity(Main)
End If
Now in the Main Activity from the Json string I am trying to rebuild the list structure:
B4X:
Dim czytaj As String = File.ReadString(File.DirInternal,"pliktekst.txt")
    Dim parser As JSONParser
    parser.Initialize(czytaj)
    Dim Root As List = parser.NextArray
    For Each col As String In Root
Next
Here are all the things I do with the code ... Now the question is, how else can I get it to work as expected ...


P.S
Here, for formalities, the code that adds an item to the list..
B4X:
Sub Butt3_Click
    
    Dim sf As Object = xui.Msgbox2Async("Dodać Nowy Moduł", "Uwaga", "Tak", "Wyjdź", "Nie", Null)'zmaias null możesz przekazać icon
    Wait For (sf) Msgbox_Result (Result As Int)
    If Result = xui.DialogResponse_Positive Then
        
        Dim lista1 As List
        Dim raf As RandomAccessFile
        raf.Initialize(File.DirInternal, "plik1.dat", False)
        lista1 = raf.ReadObject(0)
        idx=lista1.Size+1

        lista1.Add(Createmodultemp("cisni_temp_wilgot.","Moduł Ciś.Temp.Wilg.", "192.168.1.100",1024,"T1","T2","T3","T4",idx,1,"temciswilg2.png",1,2,3,4,0))

        Dim raf As RandomAccessFile
        raf.Initialize2(File.DirInternal, "plik1.dat", False, False)
        raf.WriteObject(lista1, False, 0)
        raf.Close
        Activity.Finish
        StartActivity(Me)
    End If
    
End Sub

Public Sub Createmodultemp (it As String, nm As String, ad As String, pt As Int, n1 As String, n2 As String, n3 As String, n4 As String, id As Int, ob As Int, ik As String, o1 As Int, o2 As Int, o3 As Int, o4 As Int, Mw As Int) As modultemp
    Dim t1 As modultemp
    t1.Initialize
    t1.it = it
    t1.nm = nm
    t1.ad = ad
    t1.pt = pt
    t1.n1 = n1
    t1.n2 = n2
    t1.n3 = n3
    t1.n4 = n4
    t1.id = id
    t1.ob = ob
    t1.ik = ik
    t1.o1 = o1
    t1.o2 = o2
    t1.o3 = o3
    t1.o4 = o4
    t1.Mw = Mw
    Return t1
End Sub
 
Last edited:
Upvote 0

kisoft

Well-Known Member
Licensed User
Longtime User
I think I understand where my mistake lies ... It is about such action ...

Thanks to everyone for the answers and for guiding me ...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…