How do I convert a JSON string back to a C2 array?

Get help using Construct 2

Post » Mon May 25, 2015 7:06 pm

I have my string, json_encode'd from PHP. It looks like this.

[{"Name":"Alford County","League":"Watnall Premier","ID":1},{"Name":"Highbridge County","League":"Watnall Premier","ID":3}]

That's 2 rows, with 3 values each. I want to break it down into a usable form in C2. I was thinking that I could just plonk it into an array with Array -> Load From JSON string but I get nothing in my array when I do that.

I thought there was something to do this automatically, but maybe I'm imagining it. Will I need to break the string down myself with 'tokenat' etc?
B
26
S
6
G
4
Posts: 456
Reputation: 5,853


Post » Mon May 25, 2015 11:03 pm

Thanks, that looks really useful in a lot of areas.

I assume though that this means that you can't do what I'm asking, without a plugin? It can't be done directly from C2?
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Tue May 26, 2015 1:47 am

If Ajax received this string

Code: Select all
[{"Name":"Alford County","League":"Watnall Premier","ID":1},{"Name":"Highbridge County","League":"Watnall Premier","ID":3}]

I think,not...
to set Array directly from Json...you need something like :

Code: Select all
{"c2array":true,"size":[2,3,1],"data":[[["Alford County"],["Watnall Premier"],[1]],[["Highbridge County"],["Watnall Premier"],[3]]]}


..but I could be wrong :?
B
67
S
24
G
7
Posts: 1,518
Reputation: 11,072

Post » Tue May 26, 2015 3:45 pm

Yes, I think you're right. I'll try the plugin or use my own methods for sending / splitting data. Thanks.
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Tue May 26, 2015 8:17 pm

I had to write a Java object to convert from Java arrays into Construct-friendly JSON.

This is my half-way solution (it only converts to a one-dimensional Width-based C2 array):

Code: Select all
public class C2 {
   //-------------------------------------------------------------------------------------------------------------------
   // Method to output Construct-compatible JSON Array structures
   //-------------------------------------------------------------------------------------------------------------------
   public static String outputToC2Array(ArrayList<String> list){
      
      StringBuilder sb = new StringBuilder("");
      try{
         if(list!=null && list.size()>0){
            sb.append("{\\\"c2array\\\":true,\\\"size\\\":[" + list.size() + ",1,1],\\\"data\\\":[");
            
            for(int i=0 ; i < list.size() ; i++){
               sb.append("[[" + list.get(i) + "]]");
               if(i < list.size() - 1) sb.append(","); //skips the last comma
            }            
            sb.append("]}");
         }
         return sb.toString().replaceAll("\\\\\"", "\"");
      } finally {
         sb = null;
      }
   }
   //-------------------------------------------------------------------------------------------------------------------
   // Method to reconstruct arrays from Construct-compatible JSON strings
   //-------------------------------------------------------------------------------------------------------------------
   public static ArrayList<String> arrayFromC2jsonString(String c2json){
      ArrayList<String> returnList = new ArrayList<String>();
      try{
         if(c2json.contains("data\":")){
            String subS = c2json.substring(c2json.indexOf("data\":", 0)+9, c2json.length()-4);
            String[] pieces = subS.split("\\]\\],\\[\\[");
            for(String piece : pieces){
               returnList.add(piece);
            }
         }
         return returnList;
      }finally{
         returnList = null;
      }
   }
}


Basically, I just parse the array into a C2-friendly JSON string.
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,857

Post » Tue May 26, 2015 10:05 pm

That's a thought, doing it at the PHP end, instead of breaking it up at the C2 end. Mind you, I'm FAR more comfortable with BASIC in C2 than I am in PHP. I find myself spending 15 minutes on just about EVERY command in PHP because I'm so unfamiliar with the syntax.
B
26
S
6
G
4
Posts: 456
Reputation: 5,853

Post » Wed May 27, 2015 4:44 pm

Yea, but PHP has StackOverflow at it's back. ;)
https://www.ravenheart.ca/home
I don't check the forums much anymore, but I will receive an email for PMs.

"Someone once told me I bite off more than I can chew...

I told them I would rather choke on greatness than nibble on mediocrity."
B
23
S
6
G
1
Posts: 1,419
Reputation: 4,857

Post » Thu May 28, 2015 7:41 pm

Some sample code from one of my applications that does exactly this :-

Code: Select all
// Reformat hiscores into C2 compatible format
$cnt = 1;
$cnt = count($hiscores);
for ($i = 0; $i < $cnt; $i++) {
      $scores[$i][0][0] = $hiscores [$i];
}

$content->c2array = "true";
$content->data = $scores;
$content->size[0] = $cnt;
$content->size[1] = 1;
$content->size[2] = 1;

$encoded_content = json_encode($content);


I've tried to remove any code specific to my application.
B
13
S
2
G
1
Posts: 22
Reputation: 760


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 9 guests