[SOLVED] How do I parse JSON object?

Get help using Construct 2

Post » Mon Jun 30, 2014 9:39 pm

This is formatted JSON file:

Code: Select all
{"data":{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"},"success":true,"status":200}



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: Select all
var text = '{"name":"John Johnson","street":"Oslo West 16","phone":"555 1234567"}'

var reference = JSON.parse(text);

reference.street;



and I got "0" in return. Can you help me?

Thanks in advance.
Last edited by Joannesalfa on Wed Jul 02, 2014 10:06 pm, edited 1 time in total.
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,371

Post » Mon Jun 30, 2014 10:08 pm

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"
help1.png


Here is the plugin and the example capx
storage.zip
You do not have the required permissions to view the files attached to this post.
B
20
S
7
G
1
Posts: 221
Reputation: 2,077

Post » Mon Jun 30, 2014 10:50 pm

ImageImageImage
B
71
S
21
G
230
Posts: 3,688
Reputation: 128,847

Post » Mon Jun 30, 2014 10:51 pm

@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?
You do not have the required permissions to view the files attached to this post.
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,371

Post » Mon Jun 30, 2014 10:52 pm

blackhornet wrote:Have you seen this: plugin-json-import-export-generate-edit-inspect_t100042


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.
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,371

Post » Wed Jul 02, 2014 5:11 am

Bump
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,371

Post » Wed Jul 02, 2014 11:00 am

Bump
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,371

Post » Wed Jul 02, 2014 2:06 pm

Code: Select all
Browser.ExecJS("(function() {
  var text = '{""name"":""John Johnson"",""street"":""Oslo West 16"",""phone"":""555 1234567""}';
  var reference = JSON.parse(text);
  return reference.street;
}());")
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,650

Post » Wed Jul 02, 2014 10:06 pm

@ramones good one! it works after all, I didn't know it's missing "return" thank you!
B
97
S
35
G
29
Posts: 3,139
Reputation: 28,371


Return to How do I....?

Who is online

Users browsing this forum: jobel, plinkie and 28 guests