German Wer kann mir helfen den String zu parsen?

Wolli013

Well-Known Member
Licensed User
Longtime User
Wer kann mir unter die Arme greifen, bekomme immer eine Fehlermeldung.

Der String dem ich vom Server erhalte:

{"message":"accurate","cod":"200","count":1,"list":[{"id":2906121,"name":"Herford","coord":{"lon":8.68333,"lat":52.133331},"main":{"temp":22.71,"pressure":1016,"humidity":40,"temp_min":20.56,"temp_max":25.1},"dt":1462535781,"wind":{"speed":6.2,"deg":130},"sys":{"country":"DE"},"clouds":{"all":40},"weather":[{"id":802,"main":"Clouds","description":"überwiegend bewölkt","icon":"03d"}]}]}

Die Fehlermeldung die kommt:

main_jobdone (java line: 368)
java.lang.RuntimeException: JSON Array expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:62)
at anywheresoftware.b4a.wetter.main._jobdone(main.java:368)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:169)
at anywheresoftware.b4a.keywords.Common$5.run(Common.java:996)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
java.lang.RuntimeException: JSON Array expected.

Mein Code:

B4X:
Sub Process_Globals
Dim HttpJob1 As HttpJob
End Sub

Sub Globals
    Private LabelID As Label
    Private LabelName As Label
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("wetter")
    HttpJob1.Initialize("Wetter", Me)
    HttpJob1.download("http://api.openweathermap.org/data/2.5/find?q=Herford&type=accurate&units=metric&lang=de&APPID=XXXXXXXXXXXXXX")
End Sub

Sub Activity_Resume
End Sub

Sub JobDone(Job As HttpJob)
If Job.Success = True Then
    Dim res As String
        res = Job.GetString
        Log("Wetterdaten vom Server: " & res)
       
        Dim parser As JSONParser
        parser.Initialize(res)
                Dim Wetter As List
                Wetter = parser.NextArray 'returns a list with maps
                For i = 0 To Wetter.Size - 1
                    Dim m As Map
                    m = Wetter.Get(i)
                    LabelID = m.Get("id")
                    LabelName = m.Get("name")
                    'usw usw usw
                Next
    Else
        ToastMessageShow("Error: " & Job.ErrorMessage, True)
    End If
    Job.Release
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
http://basic4ppc.com:51042/json/index.html

B4X:
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim count As Int = root.Get("count")
Dim cod As String = root.Get("cod")
Dim message As String = root.Get("message")
Dim list As List = root.Get("list")
For Each collist As Map In list
Dim dt As Int = collist.Get("dt")
Dim coord As Map = collist.Get("coord")
Dim lon As Double = coord.Get("lon")
Dim lat As Double = coord.Get("lat")
Dim name As String = collist.Get("name")
Dim weather As List = collist.Get("weather")
For Each colweather As Map In weather
  Dim icon As String = colweather.Get("icon")
  Dim description As String = colweather.Get("description")
  Dim main As String = colweather.Get("main")
  Dim id As Int = colweather.Get("id")
Next
Dim main As Map = collist.Get("main")
Dim temp As Double = main.Get("temp")
Dim temp_min As Double = main.Get("temp_min")
Dim humidity As Int = main.Get("humidity")
Dim pressure As Int = main.Get("pressure")
Dim temp_max As Double = main.Get("temp_max")
Dim id As Int = collist.Get("id")
Dim clouds As Map = collist.Get("clouds")
Dim all As Int = clouds.Get("all")
Dim sys As Map = collist.Get("sys")
Dim country As String = sys.Get("country")
Dim wind As Map = collist.Get("wind")
Dim deg As Int = wind.Get("deg")
Dim speed As Double = wind.Get("speed")
Next
 
Top