Page 1 of 1

Trying to insert JSON data

Posted: Tue Feb 04, 2025 1:13 pm
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.

Re: Trying to insert JSON data

Posted: Tue Feb 04, 2025 2:24 pm
by Fred
Are you trying to insert or extract ? Your code looks like you trying to get the info from the JSON (ie: extract)

Re: Trying to insert JSON data

Posted: Tue Feb 04, 2025 5:46 pm
by Joubarbe
Oh my god... I'm so silly! Haha! Sorry! I always confuse both extract and insert.

Re: Trying to insert JSON data

Posted: Wed Feb 05, 2025 4:31 am
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:

Re: Trying to insert JSON data

Posted: Wed Feb 05, 2025 4:39 pm
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: