Android Question jsonparser or replace

Tom1s

Member
Licensed User
Longtime User
Hi
I have json that gives unixtime and value. I tried to get it to maps but no success. Colroot gives an error.

I would like to change the unixtime to ticks in each value. How can I do it? Then I pass the string to graph.
Now all i can have is this:
(ArrayList) [[1434672000000, 44.560001373291], [1434672086000, 44.560001373291], [1434672172000, 44.5]]
 

Tom1s

Member
Licensed User
Longtime User
Yes I have tried it.. it gives:
B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As List = parser.NextArray
For Each colroot As List In root
For Each colcolroot As String In colroot
Next
Next

And error in code..or then
Parsing code. (0.02s)
Compiling code. Error
Error compiling program.
Error description: Current declaration does not match previous declaration.
Occurred on line: 370
For Each colroot As List In root
Word: colroot
 
Upvote 0

Tom1s

Member
Licensed User
Longtime User
I can't understand why I didnt see that.:oops: b4a highlighted For Each colroot As List In root ...next so i assumed there was the issue. Thanks.
 
Upvote 0

Tom1s

Member
Licensed User
Longtime User
Now I got something working. Somehow i see it much easier if there is proper json that i can parse in maps.

But now I have this root2 list: (ArrayList) [[2015.06.14, 69.809997558594],[.....
It would work without (arraylist) at the beginning in chart.
JsonGenerator accepts a map. What should I do to have a string like [[2015.06.14, 69.809997558594],[.... ]]
Perhaps i could try to convert it to google charts type string..


B4X:
dim jsondata As String = job.GetString
Dim parser As JSONParser
parser.Initialize(jsondata)
Dim root2 As List = parser.NextArray

For Each colroot2 As List In root2
For Each colcolroot As String In colroot2
Next
Dim unixdate As Long =colroot2.Get(0)
Dim date As String = GetDateFromUnixMilis(unixdate)
next
log(root2)


Private Sub GetDateFromUnixMilis(UnixTimeMilis As Long) As String

Dim targetDate As Long = DateUtils.UnixTimeToTicks(UnixTimeMilis / 1000)
'DateTime.DateFormat = "yyyy.MM.dd HH:mm:ss"
DateTime.DateFormat = "yyyy.MM.dd HH:mm"
DateTime.TimeFormat = "HH:mm:ss"
DateTime.SetTimeZone(0)
Return (DateTime.Date(targetDate))  '25.03.2014
'Return (DateTime.Time(targetDate))  '10:52:50

End Sub
 
Last edited:
Upvote 0

Tom1s

Member
Licensed User
Longtime User
Thanks. Here if somebody needs: Not the nicest code but it takes the jason data and converts unix time to date and then back to json.

B4X:
Dim jsondata As String = job.GetString
Dim parser As JSONParser
parser.Initialize(jsondata)
Dim root2 As List = parser.NextArray

For Each colroot2 As List In root2
For Each colcolroot As String In colroot2
Next
Dim unixdate As Long =colroot2.Get(0)
Dim date As String = GetDateFromUnixMilis(unixdate)
Dim date2 As String = QUOTE & date & QUOTE
colroot2.Set(0,date2)
Next
Dim coder As JSONGenerator
Log(root2)
coder.Initialize2(root2)
json=coder.ToString

Private Sub GetDateFromUnixMilis(UnixTimeMilis As Long) As String

Dim targetDate As Long = DateUtils.UnixTimeToTicks(UnixTimeMilis / 1000)
'DateTime.DateFormat = "yyyy.MM.dd HH:mm:ss"
DateTime.DateFormat = "yyyy.MM.dd HH:mm"
DateTime.TimeFormat = "HH:mm:ss"
DateTime.SetTimeZone(0)
Return (DateTime.Date(targetDate))  '25.03.2014
'Return (DateTime.Time(targetDate))  '10:52:50

End Sub
 
Upvote 0
Top