Json string with ajas to array

0 favourites
  • 8 posts
From the Asset Store
This collection of music is the beautiful performance of rather small strings ensemble (8 first violins, 6 second violin
  • Hello, I have a dataBase with a many variable.

    View dataBase : noelshack.com/2017-16-14927 ... pture4.jpg

    Texte Json :

    [{"id":1,"name":"Jeffrey","score":5}, {"id":2,"name":"Lisa","score":72}, {"id":3,"name":"Pamela","score":31}, {"id":4,"name":"Anna","score":67}, {"id":5,"name":"Antonio","score":28}, {"id":6,"name":"Judith","score":45}, {"id":7,"name":"Billy","score":88}, {"id":8,"name":"Amy","score":76}, {"id":9,"name":"Theresa","score":69}, {"id":10,"name":"Kathryn","score":25}]

    I have a file php

    <?php
        
        try
        {
    		$bdd = new PDO('mysql:host=sdf;dbname=sdf;charset=utf8', 'sdf', 'qsdf');
        }
        catch(Exception $e)
        {
            die('Erreur : '.$e->getMessage());
        }
     
    	$username = $_GET['fname'];
    	$password = $_GET['fpass'];
    	
    
    		$requser = $bdd->prepare("SELECT * FROM AnimesListe WHERE Pseudo = ? AND Mdp = ?");
    		$requser->execute(array($username, $password));
    		
    	$userinfo = $requser->fetch();
    	echo $userinfo['Anime'];
    ?>
    [/code:388kbo2q]
    
    I recover string with ajax on construct2.
    
    https://www.noelshack.com/2017-16-14927 ... apture.jpg
    
    But a probleme is that I Happen convert Json string to a array in construct ? 
    
    https://image.noelshack.com/fichiers/20 ... pture2.jpg
    
    Desired outcome : https://www.noelshack.com/2017-16-14927 ... pture3.jpg
    
    Thank you for help and I'm sorry for my english
  • Too bad your forum reputation isn't high enough, I couldn't see any image you posted.

    Your JSON represent an array of objects, which Construct can't interpret. You will have to modify your PHP so it returns something that looks like this :

    {"c2array":true,"size":[10,1,1],"data":[[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]]]}[/code:dcdlqzs8]
    This is the result of a "AsJson" from an empty array.
    
    edit: If it can be of help, this is how your example data should look like :
    [code:dcdlqzs8]{"c2array":true,"size":[10,3,1],"data":[
    [[1],["Jeffrey"],[5]],
    [[2],["Lisa"],[72]],
    [[3],["Pamela"],[31]],
    [[4],["Anna"],[67]],
    [[5],["Antonio"],[28]],
    [[6],["Judith"],[45]],
    [[7],["Billy"],[88]],
    [[8],["Amy"],[76]],
    [[9],["Theresa"],[69]],
    [[10],["Kathryn"],[25]]
    ]}[/code:dcdlqzs8]
  • Also if you are loading from a string / text json to array you need double quotes on strings and whole json in quotes

    using example from above notice the double quotes on all strings including "array" "true" "size"

    "{""c2array"":true,""size"":[10,3,1],""data"":[ [[1],[""Jeffrey""],[5]],

    [

    [2],[""Lisa""],[72]], [

    [3],[""Pamela""],[31]], [

    [4],[""Anna""],[67]], [

    [5],[""Antonio""],[28]], [

    [6],[""Judith""],[45]], [

    [7],[""Billy""],[88]], [

    [8],[""Amy""],[76]], [

    [9],[""Theresa""],[69]], [

    [10],[""Kathryn""],[25]]

    ]}"

  • JSON v1.2

    [
    	{
    		"id": 1,
    		"name": "Jeffrey",
    		"score": 5
    	},
    	{
    		"id": 2,
    		"name": "Lisa",
    		"score": 72
    	},
    	{
    		"id": 3,
    		"name": "Pamela",
    		"score": 31
    	},
    	{
    		"id": 4,
    		"name": "Anna",
    		"score": 67
    	},
    	{
    		"id": 5,
    		"name": "Antonio",
    		"score": 28
    	},
    	{
    		"id": 6,
    		"name": "Judith",
    		"score": 45
    	},
    	{
    		"id": 7,
    		"name": "Billy",
    		"score": 88
    	},
    	{
    		"id": 8,
    		"name": "Amy",
    		"score": 76
    	},
    	{
    		"id": 9,
    		"name": "Theresa",
    		"score": 69
    	},
    	{
    		"id": 10,
    		"name": "Kathryn",
    		"score": 25
    	}
    ][/code:3rsn48un][img="http://lookpic.com/O/i2/986/ol6Kf5TW.png"][url=https://app.box.com/s/wb4ct28xo186hv5kxt997pnrf5i5pe3o]json2array.capx[/url]
  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • nahima

    You don't even need to set the size of Array or loop it to get the data into array, the following will do:

    On start of layout: AJAX|Request dataBase.json (tag "data")

    AJAX | On "data" completed: Array|Load from JSON string AJAX.LastData

    This should automatically resize the array for you and load in the data.

  • But I dont have a file json but this a variable text in Database :

    drive.google.com/file/d/0B8A0oQ8zbyz5Tm5aYU9HNzlfXzA/view

    I have try to replace :

    [{"id":1,"name":"Jeffrey","score":5}, {"id":2,"name":"Lisa","score":72}, {"id":3,"name":"Pamela","score":31}, {"id":4,"name":"Anna","score":67}, {"id":5,"name":"Antonio","score":28}, {"id":6,"name":"Judith","score":45}, {"id":7,"name":"Billy","score":88}, {"id":8,"name":"Amy","score":76}, {"id":9,"name":"Theresa","score":69}, {"id":10,"name":"Kathryn","score":25}]

    in

    "{""c2array"":true,""size"":[10,3,1],""data"":[ [[1],[""Jeffrey""],[5]],

    [

    [2],[""Lisa""],[72]], [

    [3],[""Pamela""],[31]], [

    [4],[""Anna""],[67]], [

    [5],[""Antonio""],[28]], [

    [6],[""Judith""],[45]], [

    [7],[""Billy""],[88]], [

    [8],[""Amy""],[76]], [

    [9],[""Theresa""],[69]], [

    [10],[""Kathryn""],[25]]

    ]}"

    But is not work ?

    result :

    drive.google.com/file/d/0B8A0oQ8zbyz5N09UQllZUmNiQlE/view

    Code Construct2

    drive.google.com/open

  • It'is Good I have use a solution korbaach.

    Thanks you for help

  • I have an other question, I want convert Array.Json to true Json :

    Array.json :

    "{""c2array"":true,""size"":[10,3,1],""data"":[
    [[1],[""Jeffrey""],[5]],
    [[2],[""Lisa""],[72]],
    [[3],[""Pamela""],[31]],
    [[4],[""Anna""],[67]],
    [[5],[""Antonio""],[28]],
    [[6],[""Judith""],[45]],
    [[7],[""Billy""],[88]],
    [[8],[""Amy""],[76]],
    [[9],[""Theresa""],[69]],
    [[10],[""Kathryn""],[25]]
    ]}"
    
    [/code:1dbfckz6]
    
    to 
    True Json :
    
    [code:1dbfckz6]
    [
       {
          "id": 1,
          "name": "Jeffrey",
          "score": 5
       },
       {
          "id": 2,
          "name": "Lisa",
          "score": 72
       },
       {
          "id": 3,
          "name": "Pamela",
          "score": 31
       },
       {
          "id": 4,
          "name": "Anna",
          "score": 67
       },
       {
          "id": 5,
          "name": "Antonio",
          "score": 28
       },
       {
          "id": 6,
          "name": "Judith",
          "score": 45
       },
       {
          "id": 7,
          "name": "Billy",
          "score": 88
       },
       {
          "id": 8,
          "name": "Amy",
          "score": 76
       },
       {
          "id": 9,
          "name": "Theresa",
          "score": 69
       },
       {
          "id": 10,
          "name": "Kathryn",
          "score": 25
       }
    ]
    [/code:1dbfckz6]
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)