[ Json ] i'm picking up a json file

Just starting out? Need help? Post your questions and find answers here.
User avatar
skinkairewalker
Enthusiast
Enthusiast
Posts: 782
Joined: Fri Dec 04, 2015 9:26 pm

[ Json ] i'm picking up a json file

Post by skinkairewalker »

Hi guys, I'm having a lot of trouble parsing this Json File ... can someone give me a light?

This is a Json File :
https://www.codepile.net/pile/j8kpmY2y
Cyllceaux
Enthusiast
Enthusiast
Posts: 511
Joined: Mon Jun 23, 2014 1:18 pm

Re: [ Json ] i'm picking up a json file

Post by Cyllceaux »

easy :)

Code: Select all

EnableExplicit

Structure featured
	id.s
	name.s
EndStructure

Structure endingDates
	daily.s
	featured.s
EndStructure

Structure strU
	uid.s
	fullShop.s
	endingDates.endingDates
	List featured.featured()
EndStructure

Define json=LoadJSON(#PB_Any,"Untitled-1.json")
Define *str.strU=AllocateStructure(strU)

If json
	ExtractJSONStructure(JSONValue(json),*str,strU)
	FreeJSON(json)
Else
	Debug JSONErrorMessage()
EndIf
This is the fasted an easiest way. The structure "featured" has to be improved by you.
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: [ Json ] i'm picking up a json file

Post by infratec »

Code: Select all

EnableExplicit

Structure GradiantStructure
  start.s
  stop.s
EndStructure

Structure CustomColorsStructure
  background.s
  gradiant.GradiantStructure
EndStructure

Structure VideoStructure
  type.s
  url.s
EndStructure

Structure ImgStructure
  icon.s
  ft.s
  background.s
  full_background.s
EndStructure

Structure otherItemsDetailsStructure
  id.s
  name.s
  description.s
  type.s
  rarity.s
  internalRarity.s
  img.ImgStructure
EndStructure

Structure featuredStructure
  id.s
  name.s
  description.s
  type.s
  rarity.s
  internalRarity.s
  price.f
  priceNoDiscount.f
  categories.s
  priority.i
  banner.s
  offer.s
  releaseDate.s
  lastAppearance.s
  interest.f
  giftAllow.i
  buyAllowed.i
  image.s
  icon.s
  full_background.s
  List items.s()
  List otherItemsDetails.otherItemsDetailsStructure()
  List videos.VideoStructure()
  customColors.CustomColorsStructure
EndStructure

Structure EndingDatesStructure
  daily.s
  featured.s
EndStructure

Structure TestJSONStructure
  uid.s
  fullShop.i
  endingDates.EndingDatesStructure
  List featured.featuredStructure()
EndStructure


Define Filename$, File.i, JSON$, JSON.i, Format.i
Define TestJSON.TestJSONStructure


Filename$ = OpenFileRequester("Choose a JSON file", "", "JSON|*.json", 0)
If Filename$ <> ""
  File = ReadFile(#PB_Any, Filename$)
  If File
    Format = ReadStringFormat(File)
    
    If Format = #PB_Ascii Or Format = #PB_UTF8 Or Format = #PB_Unicode
      
      JSON$ = ReadString(File, #PB_File_IgnoreEOL | Format)
      
      JSON = ParseJSON(#PB_Any, JSON$)
      If JSON
        ExtractJSONStructure(JSONValue(JSON), @TestJSON, TestJSONStructure)
        Debug TestJSON\uid
        Debug TestJSON\fullshop
        Debug TestJSON\endingDates\daily
        Debug TestJSON\endingDates\featured
        ForEach TestJSON\featured()
          Debug TestJSON\featured()\id
          Debug TestJSON\featured()\name
          ForEach TestJSON\featured()\items()
            Debug "Items: " + TestJSON\featured()\items()
          Next
        Next
      EndIf
    Else
      Debug "Unknown file format"
    EndIf
  EndIf
EndIf
Last edited by infratec on Fri Oct 23, 2020 7:17 am, edited 1 time in total.
User avatar
TI-994A
Addict
Addict
Posts: 2740
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: [ Json ] i'm picking up a json file

Post by TI-994A »

From PureBasic JSON: A Quick Tutorial:

Code: Select all

#json = 0

Declare jsonRetrieveMembers(jsonObjectValue)
Declare jsonArrayRetrieveElements(jsonObjectValue)

Procedure jsonRetrieveMembers(jsonObjectValue)
 
  ; retrieve the json object members
  If ExamineJSONMembers(jsonObjectValue)
   
    ; iterate through the json object members
    While NextJSONMember(jsonObjectValue)
     
      ; determine the type of value stored in the member
      jsonValueType = JSONType(JSONMemberValue(jsonObjectValue))
     
      ; retrieve the key name of the member
      jsonMemberKeyName.s = JSONMemberKey(jsonObjectValue)
     
      ; retrieve the key-value of each member according
      ; to the respective data type and display them
      If jsonValueType = #PB_JSON_String
       
        ; retrieve & display the string value of the member
        Debug " " + jsonMemberKeyName + " = " +
              GetJSONString(JSONMemberValue(jsonObjectValue))
       
      ElseIf jsonValueType = #PB_JSON_Number
       
        ; retrieve & display the numeric value of the member
        Debug " " + jsonMemberKeyName + " = " +
              GetJSONDouble(JSONMemberValue(jsonObjectValue))       
       
      ElseIf jsonValueType = #PB_JSON_Object
       
        Debug " " + jsonMemberKeyName + " = JSON Object"
        jsonRetrieveMembers(JSONMemberValue(jsonObjectValue))
       
      ElseIf jsonValueType = #PB_JSON_Array
       
        jsonArrayRetrieveElements(jsonObjectValue)
       
      EndIf     
     
    Wend
   
  EndIf
 
EndProcedure

Procedure jsonArrayRetrieveElements(jsonObjectValue)
 
  ; retrieve the key name of the member
  jsonMemberKeyName.s = JSONMemberKey(jsonObjectValue)
 
  ; get the size of the array member
  jArraySize = JSONArraySize(JSONMemberValue(jsonObjectValue))
  Debug " " + jsonMemberKeyName + " = Array of size " + Str(jArraySize)
 
  ; get the elements of the array member
  For i = 0 To (jArraySize - 1)
   
    jsonArrayElement = GetJSONElement(JSONMemberValue(jsonObjectValue), i)
    jsonValueType = JSONType(jsonArrayElement)         
   
    If jsonValueType = #PB_JSON_String
     
      Debug " > array element " + Str(i) + " = " + GetJSONString(jsonArrayElement)
     
    ElseIf jsonValueType = #PB_JSON_Number
     
      Debug " > array element " + Str(i) + " = " + Str(GetJSONDouble(jsonArrayElement))
     
    ElseIf jsonValueType = #PB_JSON_Array
     
      Debug " > array element " + Str(i) + " = JSON Array"
      jsonArrayRetrieveElements(jsonArrayElement)
     
    ElseIf jsonValueType = #PB_JSON_Object
     
      Debug " > array element " + Str(i) + " = JSON Object"           
      jsonRetrieveMembers(jsonArrayElement)
     
    EndIf
   
  Next i 
 
EndProcedure

; load json object data from file
jsonFilename$ = OpenFileRequester("Select JSON file", "", "JSON|*.json", 0)

If jsonFilename$ <> "" And LoadJSON(#json, jsonFilename$)
   
  Debug "all key-value data in the json object:"
 
  ; get the values of the json object
  jsonObjectValue = JSONValue(#json)
 
  jsonRetrieveMembers(jsonObjectValue)
 
  ; clear & release the json object
  FreeJSON(#json)
 
EndIf
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
Post Reply