Android Question How to get "Field Name" from JSon Data

junaidahmed

Well-Known Member
Licensed User
Longtime User
I have an below "Json Data" in my application.I would like to know how to get "Field Name" from Json Data.
For example "CustomerId,CCode,CName"


B4X:
[{"CustomerId":1,"ccode":"TSIT","cname":"Twin - Set","add1":"","add2":"","city":"","state":"","country":"ITY","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Twin - Set","Priority":null},{"CustomerId":2,"ccode":"WOIT","cname":"Warnaco Italy SRL Italy","add1":"Via Provinciale Lucchese","add2":"Sesto Florentino","city":"50019","state":"","country":"ITY","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Warnaco Italy","Priority":null},{"CustomerId":3,"ccode":"ICFR","cname":"I Code","add1":"","add2":"","city":"","state":"","country":"FRA","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"I Code","Priority":"M"},{"CustomerId":4,"ccode":"NRGR","cname":"Nurage GMBH & Co","add1":"Kleines Gaesschen 13-15","add2":"Halle 5","city":"","state":"","country":"GMY","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Nurage","Priority":null},{"CustomerId":5,"ccode":"JGIT","cname":"JB Guanti","add1":"Niccolardi Edoardo 145","add2":"80131 Napoli","city":"","state":"","country":"ITY","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"JB Guanti","Priority":null},{"CustomerId":6,"ccode":"LCIT","cname":"Liviana Conti S.r.l","add1":"47039 Savignano Sul Rubicone","add2":"","city":"","state":"","country":"ITY","pin":"","contact1":"Antonella","contact2":"27","phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Liviana","Priority":"H"},{"CustomerId":7,"ccode":"CPIT","cname":"Caponi Pellami","add1":"Gloves SNC DI F.LLI FORINO","add2":"SALITA S.ELIA, 32/34, ","city":"80137 NAPOLI","state":"","country":"ITY","pin":"","contact1":"Marisa","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Caponi Pellami","Priority":null},{"CustomerId":8,"ccode":"GCIT","cname":"Giancarlo Caponi","add1":"Gloves SNC DI F.LLI FORINO","add2":"SALITA S.ELIA, 32/34, ","city":"80137 NAPOLI","state":"","country":"ITY","pin":"","contact1":"Antonella","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Giancalo Caponi","Priority":null},{"CustomerId":9,"ccode":"PSIT","cname":"Pakerson SRL","add1":"Via Mazzini 12","add2":"50054 Fucecchio ","city":"","state":"","country":"ITY","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Pakerson","Priority":null},{"CustomerId":10,"ccode":"HNFR","cname":"Helene Nepomiatzi ","add1":"Studio Blen - 9 cite Paradis","add2":"75010 Paris","city":"","state":"","country":"FRA","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Helene","Priority":null}]
 

Ohanian

Active Member
Licensed User
Longtime User
I have an below "Json Data" in my application.I would like to know how to get "Field Name" from Json Data.
For example "CustomerId,CCode,CName"


B4X:
[{"CustomerId":1,"ccode":"TSIT","cname":"Twin - Set","add1":"","add2":"","city":"","state":"","country":"ITY","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Twin - Set","Priority":null},{"CustomerId":2,"ccode":"WOIT","cname":"Warnaco Italy SRL Italy","add1":"Via Provinciale Lucchese","add2":"Sesto Florentino","city":"50019","state":"","country":"ITY","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Warnaco Italy","Priority":null},{"CustomerId":3,"ccode":"ICFR","cname":"I Code","add1":"","add2":"","city":"","state":"","country":"FRA","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"I Code","Priority":"M"},{"CustomerId":4,"ccode":"NRGR","cname":"Nurage GMBH & Co","add1":"Kleines Gaesschen 13-15","add2":"Halle 5","city":"","state":"","country":"GMY","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Nurage","Priority":null},{"CustomerId":5,"ccode":"JGIT","cname":"JB Guanti","add1":"Niccolardi Edoardo 145","add2":"80131 Napoli","city":"","state":"","country":"ITY","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"JB Guanti","Priority":null},{"CustomerId":6,"ccode":"LCIT","cname":"Liviana Conti S.r.l","add1":"47039 Savignano Sul Rubicone","add2":"","city":"","state":"","country":"ITY","pin":"","contact1":"Antonella","contact2":"27","phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Liviana","Priority":"H"},{"CustomerId":7,"ccode":"CPIT","cname":"Caponi Pellami","add1":"Gloves SNC DI F.LLI FORINO","add2":"SALITA S.ELIA, 32/34, ","city":"80137 NAPOLI","state":"","country":"ITY","pin":"","contact1":"Marisa","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Caponi Pellami","Priority":null},{"CustomerId":8,"ccode":"GCIT","cname":"Giancarlo Caponi","add1":"Gloves SNC DI F.LLI FORINO","add2":"SALITA S.ELIA, 32/34, ","city":"80137 NAPOLI","state":"","country":"ITY","pin":"","contact1":"Antonella","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Giancalo Caponi","Priority":null},{"CustomerId":9,"ccode":"PSIT","cname":"Pakerson SRL","add1":"Via Mazzini 12","add2":"50054 Fucecchio ","city":"","state":"","country":"ITY","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Pakerson","Priority":null},{"CustomerId":10,"ccode":"HNFR","cname":"Helene Nepomiatzi ","add1":"Studio Blen - 9 cite Paradis","add2":"75010 Paris","city":"","state":"","country":"FRA","pin":"","contact1":"","contact2":null,"phone1":"","phone2":"","fax":"","email":"","cusgroup":" ","shrtname":"Helene","Priority":null}]

Hi,

use this site :
B4X:
http://www.b4x.com:51042/json/index.html

and you'll get this code :

B4X:
Dim parser As JSONParser 
parser.Initialize(<text>) 
Dim root As List = parser.NextArray 
For Each colroot As Map In root 
 Dim country As String = colroot.Get("country") 
 Dim city As String = colroot.Get("city") 
 Dim Priority As String = colroot.Get("Priority") 
 Dim cname As String = colroot.Get("cname") 
 Dim phone2 As String = colroot.Get("phone2") 
 Dim CustomerId As Int = colroot.Get("CustomerId") 
 Dim phone1 As String = colroot.Get("phone1") 
 Dim shrtname As String = colroot.Get("shrtname") 
 Dim ccode As String = colroot.Get("ccode") 
 Dim pin As String = colroot.Get("pin") 
 Dim cusgroup As String = colroot.Get("cusgroup") 
 Dim state As String = colroot.Get("state") 
 Dim add2 As String = colroot.Get("add2") 
 Dim contact1 As String = colroot.Get("contact1") 
 Dim fax As String = colroot.Get("fax") 
 Dim add1 As String = colroot.Get("add1") 
 Dim email As String = colroot.Get("email") 
 Dim contact2 As String = colroot.Get("contact2") 
Next
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
If you are inserting the json data into a SQLite table, you have to create the table first like this:
B4X:
Dim strQuery As String="CREATE Table mytable (country TEXT, city TEXT, Priority TEXT,  cname TEXT," _
    & "phone2 TEXT, CustomerId INTEGER)"  'you have to add the other 12 fields to this statement
    SQL1.ExecNonQuery(strQuery)
Then insert the data in Ohanian code like this:
B4X:
SQL1.BeginTransaction
Dim parser As JSONParser 
parser.Initialize(<text>) 
Dim root As List = parser.NextArray 
For Each colroot As Map In root 
 Dim country As String = colroot.Get("country") 
 Dim city As String = colroot.Get("city") 
 Dim Priority As String = colroot.Get("Priority") 
 Dim cname As String = colroot.Get("cname") 
 Dim phone2 As String = colroot.Get("phone2") 
 Dim CustomerId As Int = colroot.Get("CustomerId") 
 Dim phone1 As String = colroot.Get("phone1") 
 Dim shrtname As String = colroot.Get("shrtname") 
 Dim ccode As String = colroot.Get("ccode") 
 Dim pin As String = colroot.Get("pin") 
 Dim cusgroup As String = colroot.Get("cusgroup") 
 Dim state As String = colroot.Get("state") 
 Dim add2 As String = colroot.Get("add2") 
 Dim contact1 As String = colroot.Get("contact1") 
 Dim fax As String = colroot.Get("fax") 
 Dim add1 As String = colroot.Get("add1") 
 Dim email As String = colroot.Get("email") 
 Dim contact2 As String = colroot.Get("contact2") 

    strQuery="INSERT INTO mytable values(?,?,?,?,?,?)"   'you need 18
    SQL1.ExecNonQuery2(strQuery,Array As Object(country, city, Priority, cname, phone2, CustomerId))  'you add the other 12 variables
Next
SQL1.TransactionSuccessful
SQL1.EndTransaction
 
Upvote 0

junaidahmed

Well-Known Member
Licensed User
Longtime User
No, Actually I would like to display Json data in Tableview.So I would like to get Field name dynamically and display it in tableview header...

Hope it's clear
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I would like to get Field name dynamically and display it in tableview header...
These are the 18 field names. You can get them after reading the first record because they repeat after each record.

upload_2018-5-15_14-44-46.png
 
Upvote 0

Roycefer

Well-Known Member
Licensed User
Longtime User
Is this for what you're looking?
B4X:
Dim jp As JsonParser
jp.Initialize(yourJsonText)
Dim root As List = jp.NextArray
For j = 0 to root.Size-1
  Log("j: " & j)
  Dim m As Map = root.Get(j)
  For Each key As String in m.Keys
    Dim fieldName As String = key     '<------ Here is the field name
    Dim fieldValue As String = m.Get(key)
    Log(Tab & "fieldName: " & fieldName & " value: " & fieldValue)   '<--- You would probably change this line to populate a new row in your table
  Next
Next
 
Upvote 0
Top