[SOLVED] How do I parse JSON object?

0 favourites
  • 9 posts
From the Asset Store
This is a single chapter from the "Construct Starter Kit Collection". It is the Student Workbook for its Workshop.
  • This is formatted JSON file:

    {"data":{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"},"success":true,"status":200}[/code:37m67xvj]
    
    I need to know how to parse this JSON and get "street" to set text in C2.
    
    I tried to use Browser object to execute javascript using:
    
    [code:37m67xvj]var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}'
    
    var reference = JSON.parse(text);
    
    reference.street;[/code:37m67xvj]
    
    and I got "0" in return.  Can you help me?
    
    Thanks in advance.
  • Others might have a better way but i created a plugin for me to do this as well. Its basically a 3 dimensional named array (hash table) with a ton of features. But it supports merging/loading JSON objects and retrieving data by key name. Your well come to give it to try

    Example for what you doing: (Note its a 3 dimensional array so since the JSON object is 2 dimensional the data was loaded to "default"

    [attachment=1:5ccyf6gw][/attachment:5ccyf6gw]

    Here is the plugin and the example capx

    [attachment=0:5ccyf6gw][/attachment:5ccyf6gw]

  • Have you seen this:

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • troublesum Nice plugin! I use it for workarounds. Actually, I planned to release this .capx to public without third party plugin due to user experience and it makes me so angry why Ashley didn't plan to create another plugin as JSON, another alternative to XML.

    I was thinking to use Array to store each properties and value but JSON format looks like hell to separate using token to get rid of " " and {}. Also to add width to Name and height to Defined Name.

    There is working sample to get values from .txt which it's formatted with simple and plain line. Enclosed a .capx on this post.

    I'm not sure how to convert it to JSON. Do you have better ideas?

  • Have you seen this:

    Yes, I've used it and it worked with my ugly hack.

    There a reason why I created this thread, I was looking a solution without third party plugins if is possible.

  • Bump

  • Bump

  • Browser.ExecJS("(function() {
      var text = '{""name"":""John Johnson"",""street"":""Oslo West 16"",""phone"":""555 1234567""}';
      var reference = JSON.parse(text);
      return reference.street;
    }());")[/code:vjk4iwxi]
  • ramones good one! it works after all, I didn't know it's missing "return" thank you!

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