Android Question [SOLVED]Can not convert string json [help]

androidappl

Active Member
Licensed User
Longtime User
Can not convert the following json:

B4X:
[
  {
    "Codice": "A_TEST_A        ",
    "Descrizione": "TEST ANDREA G                                                                                                                                                                                                                                                  ",
    "ultimaesecuzione": "2017-03-08 16:06:44           "
  },
  {
    "Codice": "AG_TEST_SEMAFORO",
    "Descrizione": "evento farlocco per testare la eliminazione di un semaforo                                                                                                                                                                                                     ",
    "ultimaesecuzione": "2017-01-16 09:15:30           "
  },
  {
    "Codice": "m_105_cancriconc",
    "Descrizione": "Cancellazione automatica riconciliazione                                                                                                                                                                                                                       ",
    "ultimaesecuzione": "2016-03-25 14:51:00           "
  },
  {
    "Codice": "m_105_ftpmandati",
    "Descrizione": "Download automatico da ftp a cartella locale partitario                                                                                                                                                                                                        ",
    "ultimaesecuzione": "2016-05-31 11:49:53           "
  },
  {
    "Codice": "m_105_ftppartite",
    "Descrizione": "Download automatico da ftp a cartella locale partitario                                                                                                                                                                                                        ",
    "ultimaesecuzione": "2015-02-06 15:12:38           "
  }
]

I've already tried:


B4X:
            Dim parser As JSONParser
           
            parser.Initialize(Job.GetString)
            Dim root As Map = parser.NextObject
 

DonManfred

Expert
Licensed User
Longtime User
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As Map In root
Dim ultimaesecuzione As String = colroot.Get("ultimaesecuzione")
Dim Descrizione As String = colroot.Get("Descrizione")
Dim Codice As String = colroot.Get("Codice")
Next

See: http://www.b4x.com:51042/json/index.html
 
Upvote 0

androidappl

Active Member
Licensed User
Longtime User


I already tried it faults on the assignment of root variable.
B4X:
Dim root As List = parser.NextArray
the error is:
B4X:
java.lang.RuntimeException: JSON Array expected.
 
Upvote 0

androidappl

Active Member
Licensed User
Longtime User
the code above will work for the json you posted in post #1
If it fails in your code then you are using another json i guess.

good, I realized that my job is a little different presents "/".

How can I delete it?

B4X:
"[{\"Codice\":\"A_TEST_A        \",\"Descrizione\":\"TEST ANDREA G                                                                                                                                                                                                                                                  \",\"ultimaesecuzione\":\"2017-03-08 16:06:44           \"},{\"Codice\":\"AG_TEST_SEMAFORO\",\"Descrizione\":\"evento farlocco per testare la eliminazione di un semaforo                                                                                                                                                                                                     \",\"ultimaesecuzione\":\"2017-01-16 09:15:30           \"},{\"Codice\":\"m_105_cancriconc\",\"Descrizione\":\"Cancellazione automatica riconciliazione                                                                                                                                                                                                                       \",\"ultimaesecuzione\":\"2016-03-25 14:51:00           \"}]"
 
Upvote 0

androidappl

Active Member
Licensed User
Longtime User
fact, solved. I do not know why but once the server passed it to the "response".

json slightly changing the original file.

with these changes the conversion in question is functioning properly.

B4X:
Dim json_corretto As String = ""
            json_corretto = Job.GetString.Replace("\","")
            json_corretto = json_corretto.SubString2(1,json_corretto.Length -1)
            Log(json_corretto)
           
            Dim parser As JSONParser
            parser.Initialize(json_corretto)
            Dim root As List = parser.NextArray
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
do you have a url where i can test it?

try something like this

B4X:
    Dim job As HttpJob
    job.Initialize("listfolder",Me)
    job.PostString(mApiV2url&"files/list_folder",$"{"path":"${path}"}"$) ' if you are sending json to your server too... dame for download, download2 and so on... 
    job.GetRequest.SetHeader("Content-Type", "application/json") ' probably not needed (test it)
    job.GetRequest.SetContentType("application/json") ' probably not needed (test it)
    job.GetRequest.SetContentEncoding("text/plain") ' try this
 
Upvote 0

androidappl

Active Member
Licensed User
Longtime User
unfortunately is a corporate local area network, where in fact my server (A WEB API C #) turns around and expects a post called sends an object to B4A to my server.
that through erel (slightly modified to my needs) serializeobject .net dll.

processes the request makes a query to the server where the database (sql server), and sends a JSON string to B4A client. (The problem was here, I solved it by placing the header response server side.)
That once the data displays them received.

I hope I was as clear as possible.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…