Help with JSON parsing

netchicken

Active Member
Licensed User
Longtime User
Hi, I am trying to parse the return from http://www.recipepuppy.com/api/?i=onions,garlic&q=omelet&p=3 into a listview using Json. (Its for a lesson). I have attacked it from a number of angles for a few hours and had no luck. I know its easy, but can someone have a look at it for me please?

I saved the file as a text file just to get it working.

B4X:
 'parse the json file
   Activity.LoadLayout("main")
   Dim JSON As JSONParser
   Dim Map1 As Map
   
   If File.Exists(File.DirAssets, "recipie.txt")Then
   
   JSON.Initialize(File.ReadString(File.DirAssets, "recipie.txt"))
   Map1 = JSON.NextObject
   Dim m As Map 'helper map for navigating
   
   Dim ingredients As List 'list holds all the recipies
   ingredients.Initialize 
   m = Map1.Get("results")'top level with a key: the value is everything else in the code. key:value pass it to m
   
   'm = m.Get("title")
   ingredients = m.Get("results")' get out the ingredients
   For i = 0 To ingredients.Size - 1
      m = ingredients.Get(i)
      Log(m.Get("title"))
      lvrecipie.AddSingleLine("title" & " " & "ingredients")
      
   Next
   Else
   ToastMessageShow("no file", "")
   End If
 

Attachments

  • recipie-database.zip
    9.4 KB · Views: 166

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("main")
   Dim JSON As JSONParser
   Dim Map1 As Map
   JSON.Initialize(File.ReadString(File.DirAssets, "recipie.txt"))
   Map1 = JSON.NextObject
   
   Dim ingredients As List 'list holds all the recipies
   ingredients = Map1.Get("results")'top level with a key: the value is everything else in the code. key:value pass it to m
   Dim m As Map
   For i = 0 To ingredients.Size - 1
      m = ingredients.Get(i)
      lvrecipie.AddSingleLine(m.Get("title") & m.Get("ingredients"))
   Next
End Sub

BTW, I find this tool useful when trying to understand the JSON structure: JSON Format - your online JSON Formatter
 
Upvote 0
Top