Android Question jsonparser or replace

Discussion in 'Android Questions' started by Tom1s, Jun 20, 2015.

  1. Tom1s

    Tom1s Member Licensed 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]]
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. Tom1s

    Tom1s Member Licensed User

    Yes I have tried it.. it gives:
    Code:
    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
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Based on the error message there is already another variable named colroot. Change it to a different variable.
     
    Peter Simpson likes this.
  5. Tom1s

    Tom1s Member Licensed 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.
     
  6. Tom1s

    Tom1s Member Licensed 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..


    Code:
    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: Jun 21, 2015
  7. Erel

    Erel Administrator Staff Member Licensed User

    JsonGenerator.Initialize2 accepts a List.
     
  8. Tom1s

    Tom1s Member Licensed 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.

    Code:
    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
     
  9. sorex

    sorex Expert Licensed User

    any reason why you have that nothing doing nested loop inthere? :)
     
Loading...