Android Tutorial Android JSON tutorial

Status
Not open for further replies.

roarnold

Active Member
Licensed User
Longtime User
1.txt

Where does "1.txt" from the example end up? I changes it to jinfo.txt and I can not find it. I get an error on the "initialize" command that is to create the "jinfo.txt".

When files are created in an app does the file with Dir Assets go to the "Files" folder under the app?

THanks,
R
 

manolis

Member
Licensed User
Longtime User
Shorter Json syntax for data recordsets

The json string has the syntax:

[{field1:value1,field2:value2,…,fieldx:valuex},{field1:value1,field2:value2,…,fieldx:valuex},...].


Is it possible to simplify the string according to the following syntax, in order to have less download data volume?

[{value1,value2,…,valuex},{value1,value2,…,valuex},...]
 

leonardoffsilva

Member
Licensed User
Longtime User
Please, can help me ? I have this result on a JSON consult :

{"result":[[{"Descricao":"PENEIRA ARCO MADEIRA ARROZ 50","Preco":"6"},{"Descricao":"PENEIRA ARCO MADEIRA ARROZ 55","Preco":"6,22"},{"Descricao":"PENEIRA ARCO MADEIRA ARROZ 60","Preco":"8,04"},{"Descricao":"PENEIRA ARCO MADEIRA CAFE 50","Preco":"6"},{"Descricao":"PENEIRA ARCO MADEIRA CAFE 55","Preco":"6,22"},{"Descricao":"PENEIRA ARCO MADEIRA CAFE 60","Preco":"8,04"},{"Descricao":"PENEIRA ARCO MADEIRA FEIJAO 50","Preco":"6"},{"Descricao":"PENEIRA ARCO MADEIRA FEIJAO 55","Preco":"6,22"},{"Descricao":"PENEIRA ARCO MADEIRA FEIJAO 60","Preco":"8,04"},{"Descricao":"PENEIRA ARCO MADEIRA FUBA 30","Preco":"3,9"},{"Descricao":"PENEIRA ARCO MADEIRA FUBA 40","Preco":"6,6"}]]}

how can I get the value of "Descricao" and the value of "Preco"
as string ?

ThankΒ΄s a Lot!
 

leonardoffsilva

Member
Licensed User
Longtime User
Perfect.
The two square brackets was created by an Datasnap server and now works like a charm.

ThankΒ΄s a lot!
 

Azlan

Member
Licensed User
Longtime User
Here is an example of breaking down a JSON string that is from a database table to a list of objects (class).

This example assumes there is a class called ClassEmployee that has the following properties: Name, Age, Address and the database table rows have the same columns.

With that said the code example below uses variables names for that paradigm.

B4X:
Dim EmployeeList : EmployeeList.Initialize


Private Sub GetEmployees(str As String)

  Dim parser As JSONParser : parser.Initialize(str)
  
  Dim Table As List = parser.NextArray ' B4A Vesion 2.20 variable assignment

  EmployeeList.Clear ' Required if this sub called again

  Dim ColName As String = "Value" ' B4A Vesion 2.20 variable assignment
      
  For I = 0 To Table.Size - 1
    
    Dim objEmployee As ClassEmployee : Dim Column As Map : Dim Row As List
  
    Row = Table.Get(I)  ' Gets a Row
  
    ' Get Columns
    Column = Row.Get(0) : objEmployee.Name    = Column.Get(ColName)
    Column = Row.Get(1) : objEmployee.Age     = Column.Get(ColName)
    Column = Row.Get(2) : objEmployee.Address = Column.Get(ColName)

    EmployeeList.Add(objEmployee) 'Add objEmployee to List

  Next

End Sub

Cheers
 
Last edited:

jalle007

Active Member
Licensed User
Longtime User
I need to parse JSON like this:


result should contain info about two flights objects Flight1 & Flight2


could you please give me sample how to do it , using Map object maybe ?
 

wimpie3

Well-Known Member
Licensed User
Longtime User
There seems to be a small bug in JSONGenerator.ToString : I see plenty of \n characters in the output stream. A leftover from .ToPrettyString?
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Given an unknown JSON response string, is there a simple way to parse it into a tree-node-like structure if you don't know which parse function to use (i.e response could be formatted for NextArray or NextObject and you don't know which it is)?
 

Jeffrey Cameron

Well-Known Member
Licensed User
Longtime User
Thank you, Erel, that was exactly what I was looking for.

I've created and attached an example to parse a supplied JSON string into a simple node structure, hopefully someone else can use it or cannibalize it as a starting point for something they need.

Thanks again for your help!
 

Attachments

  • JsonTree.zip
    10.8 KB · Views: 915

faisal

New Member
I want take the json from server in internet, the json created using php, how can I got the json?
I try using File.ReadString, but it does'nt work, i try using httprequest, but its same, can u give me a example json from server internet ?
thanks
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…