Android Question Help with json Generator

victormedranop

Well-Known Member
Licensed User
Longtime User
how can i create this json, has one MAP and a list.

B4X:
{
    "devid": "a6d12dc0c4c2ae3e",
    "bateria": 85,
    "latitud": 18.4309,
    "longitud": -69.97951,
    "speed": 0,
    "apkver": 1421,
    "rc": 0,
    "alarm": 0,
    "internet": 10,
    "rssi": 0,
    "product": "",
    "manufacturer": "",
    "model": "",
    "fuse_latitud": 0,
    "fuse_longitud": 0,
    "fuse_speed": 0,
    "fuse_altitud": 0,
    "packages": [
        {
            "package": "youtube"
        },
        {
            "package": "amazon"
        }
    ]
}

here i'm

B4X:
{
    "devid": "a6d12dc0c4c2ae3e",
    "bateria": 83,
    "latitud": 18.4309,
    "longitud": -69.9795,
    "speed": 0,
    "apkver": 1421,
    "rc": 0,
    "alarm": 0,
    "internet": 10,
    "rssi": 0,
    "product": "",
    "manufacturer": "",
    "model": "",
    "fuse_latitud": 18.430904,
    "fuse_longitud": -69.9795,
    "fuse_speed": 0.12503488,
    "fuse_altitud": -18.5,
    "packages": "anywheresoftware.b4a.objects.collections.JSONParser$JSONGenerator@5950d0c"
}
 
Last edited:
Solution
B4X:
Sub GetPkg As List
    Dim pm As PackageManager
   Dim res As List
   res.Initialize
   For Each p As String in pm.GetInstalledPackages
    res.Add(CreateMap("package": p))
   Next
  Return res
End Sub

data.Put("packages", GetPkg)

DonManfred

Expert
Licensed User
Longtime User
Hiding your code is the best way not to get help.
Create a map with most of the entries.
Then create a list and put two maps in it for the entries into this list.
add this list as to your 1st map with the key packages.
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
Hiding your code is the best way not to get help.
Create a map with most of the entries.
Then create a list and put two maps in it for the entries into this list.
add this list as to your 1st map with the key packages.
True. Sorry
B4X:
    Dim data As Map
    data.Initialize
    
    data.Put("devid",devid)
    data.Put("bateria",bateria)
    data.Put("latitud",latitud)
    data.Put("longitud",longitud)
    data.Put("speed",speed)
    data.Put("apkver",Application.VersionCode)
    data.Put("rc",rc)
    data.Put("alarm",alarm)
    data.Put("internet",internet)
    data.Put("rssi",0)
    data.Put("product",Product)
    data.Put("manufacturer",Manufacturer)
    data.Put("model",Model)
    data.Put("fuse_latitud",fuse_latitud)
    data.Put("fuse_longitud",fuse_longitud)
    data.Put("fuse_speed",fuse_speed)
    data.Put("fuse_altitud",fuse_altitud)
    
    Dim  d2 As JSONGenerator
    packages.Initialize
    Dim o As JSON = GetPkg
    d2.Initialize2(o)
    data.Put("packages",d2)
    

    
    Dim  d As JSONGenerator
    d.Initialize(data)
    Log(d.ToString)
    'ToastMessageShow(d.ToString,True)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
Is this what you want?

B4X:
'Dim  d2 As JSONGenerator
'packages.Initialize
'Dim o As JSON = GetPkg
'd2.Initialize2(o)
'data.Put("packages",d2)
Dim packages As List
packages.Initialize
packages.Add(CreateMap("package": "youtube"))
packages.Add(CreateMap("package": "amazon"))
data.Put("packages", packages)

'Dim  d As JSONGenerator
'd.Initialize(data)
'Log(d.ToString)
Log(data.As(JSON).ToString)
 
Upvote 0

victormedranop

Well-Known Member
Licensed User
Longtime User
B4X:
Sub GetPkg As List
    Dim pm As PackageManager
   Dim res As List
   res.Initialize
   For Each p As String in pm.GetInstalledPackages
    res.Add(CreateMap("package": p))
   Next
  Return res
End Sub

data.Put("packages", GetPkg)

Results !!!!
B4X:
{
    "devid": "a6d12dc0c4c2ae3e",
    "bateria": 100,
    "latitud": 18.4309,
    "longitud": -69.97949,
    "speed": 0,
    "apkver": 1421,
    "rc": 0,
    "alarm": 0,
    "internet": 10,
    "rssi": 0,
    "product": "",
    "manufacturer": "",
    "model": "",
    "fuse_latitud": 18.4309,
    "fuse_longitud": -69.97949,
    "fuse_speed": 0,
    "fuse_altitud": -16,
    "packages": [
        {
            "package": "com.huawei.hifolder"
        },
        {
            "package": "com.android.cts.priv.ctsshim"
        },
        {
            "package": "com.google.android.youtube"
        },
        {
            "package": "com.huawei.camera"
        },
        {
            "package": "net.gmtrd.gmt"
        },
        {
            "package": "com.huawei.permissioncontroller.overlay"
        },
        {
            "package": "com.huawei.android.tips"
        },
        {
            "package": "com.huawei.camerakit.impl"
        },
        {
            "package": "com.google.android.ext.services"
        },
        {
            "package": "com.huawei.synergy"
        },
        {
            "package": "com.huawei.android.launcher"
        },
        {
            "package": "com.android.mediacenter"
        },
        {
            "package": "com.android.providers.telephony"
        },
        {
            "package": "com.google.android.googlequicksearchbox"
        },
        {
            "package": "com.android.providers.calendar"
        },
        {
            "package": "com.huawei.featurelayer.featureframework"
        },
        {
            "package": "com.huawei.ridemode"
        },
        {
            "package": "com.huawei.health"
        },
        {
            "package": "com.huawei.hicard"
        },
        {
            "package": "com.huawei.hidisk"
        },
        {
            "package": "com.huawei.hiview"
        },
        {
            "package": "com.huawei.iaware"
        },
        {
            "package": "com.huawei.bluetooth"
        },
        {
            "package": "com.android.providers.media"
        },
        {
            "package": "games.my.zombie.shooter"
        },
        {
            "package": "com.touchtype.swiftkey"
        },
        {
            "package": "com.huawei.android.thememanager"
        },
        {
            "package": "com.huawei.android.chr"
        },
        {
            "package": "com.huawei.android.hsf"
        },
        {
            "package": "com.google.android.onetimeinitializer"
        },
        {
            "package": "com.google.android.ext.shared"
        },
        {
            "package": "com.huawei.autoinstallapkfrommcc"
        },
        {
            "package": "com.android.wallpapercropper"
        },
        {
            "package": "com.huawei.nearby"
        },
        {
            "package": "com.huawei.android.FloatTasks"
        },
        {
            "package": "com.huawei.intelligent"
        },
        {
            "package": "com.huawei.motionservice"
        },
        {
            "package": "com.huawei.appmarket"
        },
        {
            "package": "com.huawei.recsys"
        },
        {
            "package": "com.huawei.parentcontrol"
        },
        {
            "package": "com.huawei.search"
        },
        {
            "package": "com.android.documentsui"
        },
        {
            "package": "com.huawei.numberidentity"
        },
        {
            "package": "com.android.externalstorage"
        },
        {
            "package": "com.huawei.multimedia.audioengine"
        },
        {
            "package": "com.huawei.android.FMRadio"
        },
        {
            "package": "com.android.htmlviewer"
        },
        {
            "package": "com.android.companiondevicemanager"
        },
        {
            "package": "com.android.mms.service"
        },
        {
            "package": "com.huawei.android.totemweather"
        },
        {
            "package": "com.android.providers.downloads"
        },
        {
            "package": "com.huawei.gameassistant"
        },
        {
            "package": "com.google.android.apps.messaging"
        },
        {
            "package": "com.android.networkstack.inprocess"
        },
        {
            "package": "com.huawei.phoneservice"
        },
        {
            "package": "com.huawei.screenrecorder"
        },
        {
            "package": "com.huawei.himovie.overseas"
        },
        {
            "package": "com.android.partnerbrowsercustomizations.tmobile"
        },
        {
            "package": "com.huawei.videoeditor"
        },
        {
            "package": "com.huawei.securitymgr"
        },
        {
            "package": "com.google.android.overlay.gmsgsaconfig"
        },
        {
            "package": "com.google.android.configupdater"
        },
        {
            "package": "com.google.android.overlay.modules.permissioncontroller"
        },
        {
            "package": "com.android.soundrecorder"
        },
        {
            "package": "com.huawei.iconnect"
        },
        {
            "package": "com.google.android.overlay.settingsProvider"
        },
        {
            "package": "com.huawei.KoBackup"
        },
        {
            "package": "com.huawei.hiviewtunnel"
        },
        {
            "package": "com.android.providers.downloads.ui"
        },
        {
            "package": "com.android.vending"
        },
        {
            "package": "com.android.pacprocessor"
        },
        {
            "package": "com.android.simappdialog"
        },
        {
            "package": "com.android.systemui.overlay"
        },
        {
            "package": "com.hisi.mapcon"
        },
        {
            "package": "androidhwext"
        },
        {
            "package": "com.android.frameworkhwext.honor"
        },
        {
            "package": "com.android.certinstaller"
        },
        {
            "package": "com.android.carrierconfig"
        },
        {
            "package": "com.huawei.wallet.sdk.walletsdk"
        },
        {
            "package": "com.google.android.marvin.talkback"
        },
        {
            "package": "com.google.android.apps.work.oobconfig"
        },
        {
            "package": "android"
        },
        {
            "package": "com.huawei.imedia.sws"
        },
        {
            "package": "com.huawei.hiai"
        },
        {
            "package": "com.huawei.hwid"
        },
        {
            "package": "com.huawei.msdp"
        },
        {
            "package": "com.huawei.tips"
        },
        {
            "package": "com.android.contacts"
        },
        {
            "package": "com.huawei.systemserver"
        },
        {
            "package": "com.huawei.mycenter"
        },
        {
            "package": "com.android.egg"
        },
        {
            "package": "com.android.mtp"
        },
        {
            "package": "com.android.ons"
        },
        {
            "package": "com.android.stk"
        }
    ]
}
 
Upvote 0
Top