How do I convert strings into a JSON?

1 favourites
  • 12 posts
From the Asset Store
A collection of a mix of percussion and orchestral string loops, including 20 musical sketches.
  • Hey all,

    So I am trying to create this ordering system. Let's say I have 4 textboxes; sender, description, ID and recipient. The first and last one are a random string of characters. Description is a long body of text. Order is just a number (int to be exact).

    However, each user in the game will have a bunch of orders and I need to store that in an array.

    What I want to know is:

    1- How do I turn the values in the 4 textboxes into a JSON (tried to look for it but seems all info is about parsing from not converting to JSON)?

    2- Then how do I add that to an "Orders JSON array" (not sure how that is done) to write it to a server?

    Any ideas how I can achieve this?

    Tagged:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • The array expression Array.AsJSON will give you the array as a string in JSON format.

    I'm not familiar with uploading to a server. Depends what kind of server you are using. If the array isn't too large I imagine you could use a POST link to a php page with AJAX object, and the server can save it to a mysql db.

    Or if you're using something like firebase the plugin can probably send data directly.

  • The array expression Array.AsJSON will give you the array as a string in JSON format.

    I'm not familiar with uploading to a server. Depends what kind of server you are using. If the array isn't too large I imagine you could use a POST link to a php page with AJAX object, and the server can save it to a mysql db.

    Or if you're using something like firebase the plugin can probably send data directly.

    So let me see if I understand correctly, what you're saying is that I should have an Array object, inside that object I'd add my strings (the 4 of them) and then choose the Array.AsJSON and then send it over (I'll be using PlayFab so I am using its plugin for the upload)?

    If that is the case, that would not allow me to create the strings in a "key:value" format from what I understand.

    I'd like the JSON to be structured in a way that I can simply say:

    { orders [ sender: "senderID", description: "this is some order that has some long text description", orderID: 2, recipient: "recipientID" ] }

    So when I receive such a string from client1 on client2 I'd be able to take the data and display it to client2 the way I want.

    Does that make sense?

  • If you don't want to use a Json array, then you can manually create keys with the JSON object actions set path, set object, and set value actions. Then the tobeautifiedstring or the tocompactstring expressions will return your json as a string you can send.

  • If you don't want to use a Json array, then you can manually create keys with the JSON object actions set path, set object, and set value actions. Then the tobeautifiedstring or the tocompactstring expressions will return your json as a string you can send.

    Oh, sorry I think I misspoke. It isn't about what I want. I just don't see how it can be done. Since in my mind the array doesn't take a "key" just the value". So I wasn't sure how to set it up the key/value way so when I am reading back the JSON on the other side I could extract what I want.

  • Right, because an array is just a set of values with no object names, you'll need to create object names manually with the json actions manually.

    Although normally you would want to send an array as an array, and object/value pairs as is.

    Is there any reason you need to convert? Anything that can read json should also be able to read json arrays.

    Or I'm still misunderstanding.

  • Right, because an array is just a set of values with no object names, you'll need to create object names manually with the json actions manually.

    Although normally you would want to send an array as an array, and object/value pairs as is.

    Is there any reason you need to convert? Anything that can read json should also be able to read json arrays.

    Or I'm still misunderstanding.

    Oh, I don't need to convert. Basically I am trying to create this data structure in JSON:

    Orders:

    --Order1

    --- SenderID

    --- Description

    --- ID

    --Order2

    ---SenderID

    ---Description

    ---ID

    --Order3

    ---SenderID

    ---Description

    ---ID

    User logs in, sends an order and it gets added to that list on the server. Then when the other user logs in, they pull the information and get that list and I parse it to display it in a better way.

    I don't know how to get that kind of result using the JSON object or the Array object personally. I am using the PlayFab plugin which forces me to get all data currently available. Then append the order being sent. Then upload the entire thing (old + new).

    This data isn't global, it is stored per user.

  • I think I understand...

    Like I said earlier, the simplest way would be to keep your local data in an array. You can get the entire array as a json string by using the array.asjson expression. Send that to the server for storage.

    Retrieve the array.asjson from the server, and use the array load action to get it back into an array, to append and modify as you wish.

    There's no reason you need to convert the array into key:value pairs unless I'm totally missing something, like you needing to modify or access it with a third party program that can't handle json arrays for some reason.

    The other option would be to use the json object actions to set each path, object, and value, then send json.tocompactstring to the server. Then you can retrieve it and use the json parse action to load it up and be able to modify it again.

    I'd think the array method is cleaner/easier though.

    Edit: I can put together an example using local storage as the "server" later if you need.

  • I think I understand...

    Like I said earlier, the simplest way would be to keep your local data in an array. You can get the entire array as a json string by using the array.asjson expression. Send that to the server for storage.

    Retrieve the array.asjson from the server, and use the array load action to get it back into an array, to append and modify as you wish.

    There's no reason you need to convert the array into key:value pairs unless I'm totally missing something, like you needing to modify or access it with a third party program that can't handle json arrays for some reason.

    The other option would be to use the json object actions to set each path, object, and value, then send json.tocompactstring to the server. Then you can retrieve it and use the json parse action to load it up and be able to modify it again.

    I'd think the array method is cleaner/easier though.

    Edit: I can put together an example using local storage as the "server" later if you need.

    Hmm, I think I see what you mean. So the array will be 2D; X say would be an entire order. Then each cell in Y is one of the pieces of the order (sender, ID, description, etc...)? I just need to memorize which cell handles which piece of data?

  • Exactly. Memorize, or rather the program will remember for you.

    Bonus you might not even need an ID field, since the x index of the array is basically an ID. Unless the ID field refers to an external number of some sort then you would still need to have it.

  • Here's an example. You would replace the localstorage events with whatever your server uses.

    dropbox.com/s/3diz2xukc4732zg/LocalstorageSaveArrayExample.c3p

  • Here's an example. You would replace the localstorage events with whatever your server uses.

    https://www.dropbox.com/s/3diz2xukc4732zg/LocalstorageSaveArrayExample.c3p?dl=0

    Oh, wow, thank you very much. That explains it pretty well.

    I wish this forums had some kind of kudos system :D. Thanks again!

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