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

  • 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?

  • 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?

  • If Ajax received this string

    [{"Name":"Alford County","League":"Watnall Premier","ID":1},{"Name":"Highbridge County","League":"Watnall Premier","ID":3}][/code:6ivnj7lg]I think,not...
    to set  Array  directly from Json...you need something like :
    
    [code:6ivnj7lg]{"c2array":true,"size":[2,3,1],"data":[[["Alford County"],["Watnall Premier"],[1]],[["Highbridge County"],["Watnall Premier"],[3]]]}[/code:6ivnj7lg]
    
    ..but I could be wrong
  • Yes, I think you're right. I'll try the plugin or use my own methods for sending / splitting data. Thanks.

  • 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):

    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;
    		}
    	}
    }
    [/code:cwm5su9d]
    
    Basically, I just parse the array into a C2-friendly JSON string.
  • 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.

  • Yea, but PHP has StackOverflow at it's back.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Some sample code from one of my applications that does exactly this :-

    // 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);
    [/code:2sx2hb05]
    
    I've tried to remove any code specific to my application.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)