Need help with array of objects !

Just starting out? Need help? Post your questions and find answers here.
ruslanx
User
User
Posts: 46
Joined: Mon Jun 06, 2011 8:28 am

Need help with array of objects !

Post by ruslanx »

I have file job.json:

Code: Select all

[
    {
        "published": "",
        "category": "",
        "url": "",
        "skills": ""
    },
    {
        "published": "",
        "category": "",
        "url": "",
        "skills": ""
    },
    {
        "published": "",
        "category": "",
        "url": "",
        "skills": ""
    }
]
how to read values (url ...) ?
User avatar
jacdelad
Addict
Addict
Posts: 1992
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: Need help with array of objects !

Post by jacdelad »

Take a look at the JSON library, especially ParseJSON().
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
ruslanx
User
User
Posts: 46
Joined: Mon Jun 06, 2011 8:28 am

Re: Need help with array of objects !

Post by ruslanx »

after node js javascript I can't understand how to .. please give me example
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Need help with array of objects !

Post by Kiffi »

ruslanx wrote: Thu Jul 13, 2023 3:20 pm after node js javascript I can't understand how to .. please give me example
viewtopic.php?t=80283
Hygge
ruslanx
User
User
Posts: 46
Joined: Mon Jun 06, 2011 8:28 am

Re: Need help with array of objects !

Post by ruslanx »

Code: Select all

#JSON = 1

Global WorkPath$ = GetPathPart(ProgramFilename())
Global JobsFile$ = WorkPath$ + "jobs.json"


Structure JobData
  published.s
  category.s
  url.s
  skills.s
EndStructure

Global NewList J.JobData()


LoadJSON(#JSON, JobsFile$)

ExtractJSONStructure(JSONValue(#JSON), @J, JobData)
; what next ?
ruslanx
User
User
Posts: 46
Joined: Mon Jun 06, 2011 8:28 am

Re: Need help with array of objects !

Post by ruslanx »

there is an object here is array of objects
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

Re: Need help with array of objects !

Post by StarBootics »

Hello,

The modified json file :

Code: Select all

[
    {
        "published": "patato",
        "category": "food",
        "url": "www.patatoes.com",
        "skills": "minimum"
    },
    {
        "published": "patato",
        "category": "food",
        "url": "www.patatoes.com",
        "skills": "minimum"
    },
    {
        "published": "patato",
        "category": "food",
        "url": "www.patatoes.com",
        "skills": "minimum"
    }
]

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Json Example
; File Name : Json Example.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : July 13th, 2023
; Last Update : July 13th, 2023
; PureBasic code : V6.03 beta 2 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Structure Records
  
  published.s
  category.s
  url.s
  skills.s
  
EndStructure

Dim jobs.Records(3)

If LoadJSON(0, "job.json")
  
  ExtractJSONArray(JSONValue(0), jobs())
  
EndIf

For Index = 0 To 2
  Debug jobs(Index)\published
  Debug jobs(Index)\category
  Debug jobs(Index)\url
  Debug jobs(Index)\skills
  Debug ""
Next

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
ruslanx
User
User
Posts: 46
Joined: Mon Jun 06, 2011 8:28 am

Re: Need help with array of objects !

Post by ruslanx »

Code: Select all

#JSON = 1

Global WorkPath$ = GetPathPart(ProgramFilename())
Global JobsFile$ = WorkPath$ + "jobs.json"

Structure JobData
  published.s
  category.s
  url.s
  skills.s
EndStructure

NewList Jobs.JobData()

If LoadJSON(#JSON,JobsFile$)
  ExtractJSONList(JSONValue(#JSON), Jobs())
  ForEach Jobs()
    Debug Jobs()\published
    Debug Jobs()\category
    Debug Jobs()\url
    Debug Jobs()\skills
  Next
EndIf

thank you!
Post Reply