Trying to insert JSON data

Just starting out? Need help? Post your questions and find answers here.
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Trying to insert JSON data

Post by Joubarbe »

Code: Select all

Structure _Product
  base_cost.i
  spoil.i
  weight.i
EndStructure

Define json$ = ~"{\"water\":{\"base_cost\":10,\"spoil\":-1,\"weight\":1}}"

ParseJSON(0, json$)
Define value = JSONValue(0)
Define water._Product

If ExamineJSONMembers(value)
  While NextJSONMember(value)
    Define subvalue = JSONMemberValue(value)
    InsertJSONStructure(subvalue, @water, _Product)
;     ExamineJSONMembers(subvalue)
;     While NextJSONMember(subvalue)
;       Debug JSONMemberKey(subvalue) + " = " + GetJSONInteger(JSONMemberValue(subvalue))
;     Wend
  Wend
EndIf

Debug water\base_cost
Debug water\spoil
Debug water\weight
What am I doing wrong?

I've actually tried the following first, as it sounds more logic to me:

Code: Select all

InsertJSONStructure(value, @water, _Product)
(value instead of subvalue)

But then the program seems to hang in an infinite loop.

EDIT: The idea being that the JSON file would have more products, and that each product would be stored inside a map.
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Trying to insert JSON data

Post by Fred »

Are you trying to insert or extract ? Your code looks like you trying to get the info from the JSON (ie: extract)
Joubarbe
Enthusiast
Enthusiast
Posts: 714
Joined: Wed Sep 18, 2013 11:54 am
Location: France

Re: Trying to insert JSON data

Post by Joubarbe »

Oh my god... I'm so silly! Haha! Sorry! I always confuse both extract and insert.
User avatar
idle
Always Here
Always Here
Posts: 6026
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: Trying to insert JSON data

Post by idle »

Joubarbe wrote: Tue Feb 04, 2025 5:46 pm Oh my god... I'm so silly! Haha! Sorry! I always confuse both extract and insert.
tell that to my dentist! :lol:
Little John
Addict
Addict
Posts: 4802
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Trying to insert JSON data

Post by Little John »

idle wrote: Wed Feb 05, 2025 4:31 am
Joubarbe wrote: Tue Feb 04, 2025 5:46 pm Oh my god... I'm so silly! Haha! Sorry! I always confuse both extract and insert.
tell that to my dentist! :lol:
:lol:
Post Reply