How do I add data to a JSON file that was not created in C3?

0 favourites
  • 3 posts
From the Asset Store
Kids Game
$49 USD
New Sounds Added Update: 115 new sound effects added for no additional cost!
  • Hey everyone,

    I'm currently stuck with a JSON problem I can't solve. I want to add a new element into this existing JSON file, but I am struggling to recreate the JSON structure within C3.

    It's for a quiz game, the JSON file stores all the questions and I am trying to create a question editor, where I can enter new question and add them to the JSON file.

    Here's what I did so far:

    1. I created a Dictionary with the keys:

    id, question, answersArray, wrongAnswersArray, hint

    2. Grabbed some data from Input Fields and added to the Dictionary

    3. Set some data for the arrays and added the array.AsJSON as Value in the Dictionary

    My Template JSON

    { "questions": [ { "id": "0", "question": "What is that?", "answersArray": [ "answer1", "answer2" ], "wrongAnswersArray": [ "wrongAnswer1", "wrongAnswer2" ], "hint": "Lorem ipsum" }, { "id": "1", "question": "What is this?", "answersArray": [ "answer1", "answer2" ], "wrongAnswersArray": [ "wrongAnswer1", "wrongAnswer2" ], "hint": "Lorem ipsum" }, { "id": "2", "question": "What is not true?", "answersArray": [ "answer1", "answer2" ], "wrongAnswersArray": [ "wrongAnswer1", "wrongAnswer2" ], "hint": "Lorem ipsum" } ] }

    C3 Output when I log the Dictionary.AsJSON

    { "c2dictionary": true, "data": { "id": 123, "question": "What is red?", "answersArray": "{\"c2array\":true,\"size\":[1,1,1],\"data\":[[[\"Fire\"]]]}", "wrongAnswersArray": "{\"c2array\":true,\"size\":[3,1,1],\"data\":[[[\"Sea\"]],[[\"Honey\"]],[[\"Cloud\"]]]}", "hint": "It is hot." } }

  • You need to load (parse) this data into JSON object. There is an official example in C3 explaining how to do it.

    After that you can copy the data into an array or a dictionary, but it may be tricky. And the structure is pretty complex, you might need several linked dictionaries or something like that. So I don't see any benefits in doing this.

    I suggest working with JSON object directly instead, it will be much easier. JSON plugin in Construct allows to read and modify any data.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I made a example: JSON_Additem.c3p

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)