Android Question add link list to JSON with slash

abbas abedi

Member
hello
I have problem
I wanna add a link list to a json text

this is my link list :
B4X:
        LinkList.Add("/neda/live-post-image/1.jpg")
        LinkList.Add("/neda/live-post-image/2.jpg")
        LinkList.Add("/neda/live-post-image/3.jpg")


this is wrong result ("\"\/ \"") :
B4X:
{"repeated_string":{
               "value":[
                  "\"\/neda\/live-post-image\/1.jpg\"",
                  "\"\/neda\/live-post-image\/2.jpg\"",
                  "\"\/neda\/live-post-image\/3.jpg\""
               ]
            }}

this is my b4a code:
B4X:
    Private LinkListAs List
    LinkList.Initialize
        LinkList.Add("/neda/live-post-image/1.jpg")
        LinkList.Add("/neda/live-post-image/2.jpg")
        LinkList.Add("/neda/live-post-image/3.jpg")

     Private parser As JSONParser
        parser.Initialize(<JsonTEXT>)
        Private jRoot As Map = parser.NextObject
        Private Images_repeated_string As Map = jRoot.Get("repeated_string")
     
        Images_repeated_string.Put("value",LinkList)
        jRoot.Put("repeated_string",Images_repeated_string)
     
        Private JSONGenerator1 As JSONGenerator
        JSONGenerator1.Initialize(jRoot)
        NewJsonTEXT = JSONGenerator1.ToString
    Log(NewJsonTEXT)

so How can I fix it?
 
Last edited:

Daestrum

Expert
Licensed User
Longtime User
Although that is valid json the values would all have quotes around them inside the quotes for json
instead of value =
/neda/live-post-image/1.jpg
it would actually be
"/neda/live-post-image/1.jpg" as the value so you may need to remove the extraneous quotes
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
EDIT: Test 3, add more list
B4X:
    Log("----- Create Json -----")
 
    Dim LinkList1 As List
    LinkList1.Initialize
    LinkList1.Add("/neda/live-post-image/1.jpg")
    LinkList1.Add("/neda/live-post-image/2.jpg")
    LinkList1.Add("/neda/live-post-image/3.jpg")
    Dim m As Map = CreateMap("repeated_string": CreateMap("value":LinkList1))
 
    Dim sJson As String = m.As(JSON).ToString
    Log(sJson)
 
    Log("----- Add more lists -----")
 
    Dim MapJson As Map = sJson.As(JSON).ToMap
    MapJson.Get("repeated_string").As(Map).Get("value").As(List).Add("/neda/live-post-image/4.jpg")
    MapJson.Get("repeated_string").As(Map).Get("value").As(List).Add("/neda/live-post-image/5.jpg")
    MapJson.Get("repeated_string").As(Map).Get("value").As(List).Add("/neda/live-post-image/6.jpg")
 
    Dim sJson As String = MapJson.As(JSON).ToString
    Log(sJson)

1724210010689.png
 
Last edited:
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Read Json
B4X:
    Log("----- Read Json -----")
    
    Dim repeated_string As Map = sJson.As(JSON).ToMap.Get("repeated_string")
    Dim value As List = repeated_string.Get("value")
    For Each Link As String In value
        Log(Link)
    Next
1724210389687.png
 
Upvote 0

abbas abedi

Member
I think this is correct. Some special characters need to be escaped.
it would actually be
"/neda/live-post-image/1.jpg" as the value so you may need to remove the extraneous quotes

Thanks, brothers. I need to convert JSON format to string format and remove the backslashes (`\`) from the string format.

I want this result :
B4X:
"repeated_string":{
               "value":[
                  "/neda/live-post-image/1.jpg",
                  "/neda/live-post-image/2.jpg",
                  "/neda/live-post-image/3.jpg"
               ]
            }
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Thanks, brothers. I need to convert JSON format to string format and remove the backslashes (`\`) from the string format.

I want this result :
B4X:
"repeated_string":{
               "value":[
                  "/neda/live-post-image/1.jpg",
                  "/neda/live-post-image/2.jpg",
                  "/neda/live-post-image/3.jpg"
               ]
            }
This is the string you want?
B4X:
    Dim s As String=$"
    {"repeated_string":{
               "value":[
                  "\"\/neda\/live-post-image\/1.jpg\"",
                  "\"\/neda\/live-post-image\/2.jpg\"",
                  "\"\/neda\/live-post-image\/3.jpg\""
               ]
            }}"$
    Log( Regex.Replace("\\",Regex.Replace($"\\""$, s, ""),""))
 
Upvote 1

Daestrum

Expert
Licensed User
Longtime User
B4X:
    Private LinkList As List
    LinkList.Initialize
    LinkList.Add("/neda/live-post-image/1.jpg")
    LinkList.Add("/neda/live-post-image/2.jpg")
    LinkList.Add("/neda/live-post-image/3.jpg")
    Dim JsonText As String =$"{"repeated_string":{}}"$
    
    Private parser As JSONParser
    parser.Initialize(JsonText)
    Private jRoot As Map = parser.NextObject
    Private Images_repeated_string As Map = jRoot.Get("repeated_string")
     
    Images_repeated_string.Put("value",LinkList)
    jRoot.Put("repeated_string",Images_repeated_string)
    Dim newjsontext As String 
    Private JSONGenerator1 As JSONGenerator
    JSONGenerator1.Initialize(jRoot)
    newjsontext = JSONGenerator1.ToString.Replace("\/","/")
    Log(newjsontext)
 
Upvote 1
Top