0 Favourites

How do I Read a JSON File in C2?

  • Hello, I am stumped on this part and I need some help. I want an array of data (lat, long, distance, name) that can be read as a JSON file. I need help making the file and it needs to be very organized as I plan to have lots of data. It would be very helpful if I could make it look like this example:

    "Data":[ {"Lat":"1", "Long":"11"}, {"Lat":"2", "Long":"12"}, {"Lat":"3", "Long":"13"} ]

    But then I don't understand how to get Construct to read the sections(e.g. Data[0].Lat) by name.

    Is this the wrong approach? How would you make an organized JSON file like the example that can be read by construct?

    I'm completely lost so thanks for any help!

    ~ Bucktoothfrog

  • You are missing...

    {

    "c2array":true,
    "size":[13,6,1],
    "data":[[[/code:21u9uywp]
    
    I added a small tut months ago... it should steer you in the right direction.
    [url=https://www.scirra.com/tutorials/978/example-load-data-from-json-to-array-and-populate-game-card]load data from json to array and populate game card[/url]
    
    If you want organized, and are dealing with huge amount of data... this is a gem
    [url=https://www.scirra.com/store/game-making-tools/c2-data-editor-124]c2 data editor[/url]
  • Do you know if construct 2 uses a specific format in json? Can i use any syntax with json and still make it work? Also, how do i access a label in the events page? I want to have a column named lat and access it by lat[0] or something like that. Thanks for the help!

  • Yes it does.

    I would create a your array (just a few) in construct then download to json. Then use what I did and load it, then you can add all your data to that json file using the correct format.

    I attached a json file to the tut - you can look at what it should look like.

    Something like this

    {
    "c2array":true,
    "size":[x,y,z],
    "data":[[
    
    ["00"],
    ["1"],
    ["11"]
    ],[
    ["01"],
    ["2"],
    ["22"]
    ],[
    ["02"],
    ["3"],
    ["33"]
    
    ]]
    }[/code:a8hqfvt6]
  • I bought the C2 Data Editor and it works great! My only issue is I need a simple "tag" to access my array column names. For example, If{Array.At(Lat) & Array.At(Long) = 20}.

    Basically I want to access the array values with a word, not Array.At(3,0,1), for example. Is there any way I can do this? Thanks for all the help!

  • bucktoothfrog - I just added this plugin. It will probably work for what your looking for. It supports loading raw JSON directly into it and accessing the data via named indexes.

    https://www.scirra.com/forum/plugin-storage-3d-dictionary_t125861

  • bucktoothfrog - I just added this plugin. It will probably work for what your looking for. It supports loading raw JSON directly into it and accessing the data via named indexes.

    I can't seem to understand how to add an index to a column name. Can you please give an example?

  • I need to try Troublesums plugin but I've been happy with forum/ plugin-json-import-export-generate-edit-inspect_t100042

  • bucktoothfrog - The purpose of this plugin was to get away from index values and access data via names. if you can modify how you input your data into this plugin to contain only JSON objects with named keys instead an Array that is indexed it should work fine

    try loading this (Notice the "0" and "1" are strings)

    {"Data":{"0":{"lat":1,"long":11},"1":{"lat":2,"long":12}}}

    You can then retrieve the values using the get action

    Storage.get("Data","0","lat") = 1

    Storage.get("Data","0","long") = 11

    Storage.get("Data","1","lat") = 2

    Storage.get("Data","1","long") = 12

  • I see. But how do I import my already written JSON file from "C2 Data Editor" into a readable format for your plugin?

  • bucktoothfrog - Tell you what I can do... if can you send me an exact example of the JSON string you get from the C2 Data editor (I've never used it) that you want to load into my plugin ill see about making my plugin support merging it. I'm sure I can make it work and It will help others in the future that wind up trying to do the same thing

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • Ok I'll send it to you. But, let's say that I didn't have the editor. How would I write and import the file into C2? Thanks for all the help, I really appreciate it!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)
Similar Topics Posts Views Last Post
Unread hot topic
0 Favourites
Spriter/C2 - Spriter 2 Teaser!
1,800 322,286
tarek2's avatar
tarek2
Unread sticky
763 305,711
asikanddestroy's avatar
asikanddestroy
Unread hot topic
0 Favourites
Your C2 tests
566 243,743
thisisblogs's avatar
thisisblogs