Json string with ajas to array

Get help using Construct 2

Post » Thu Apr 20, 2017 8:13 pm

Message: nahima can only post plain text URLS until they have 500 rep. 4 URLS modified. Why?
Hello, I have a dataBase with a many variable.

View dataBase : https://www.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
Code: Select all
<?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'];
?>


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
B
3
Posts: 4
Reputation: 183

Post » Thu Apr 20, 2017 8:35 pm

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 :
Code: Select all
{"c2array":true,"size":[10,1,1],"data":[[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]],[[0]]]}

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: Select all
{"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]]
]}
B
67
S
28
G
24
Posts: 970
Reputation: 18,675

Post » Thu Apr 20, 2017 9:30 pm

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]]
]}"
...
B
42
S
21
G
7
Posts: 260
Reputation: 7,683

Post » Thu Apr 20, 2017 11:31 pm

JSON v1.2


Code: Select all
[
   {
      "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
   }
]


Image


json2array.capx
Image
B
64
S
23
G
7
Posts: 1,481
Reputation: 10,823

Post » Fri Apr 21, 2017 11:15 am

@nahima
@korbach

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.
B
16
S
7
G
3
Posts: 395
Reputation: 3,693

Post » Fri Apr 21, 2017 8:32 pm

Message: nahima can only post plain text URLS until they have 500 rep. 3 URLS modified. Why?
But I dont have a file json but this a variable text in Database :

https://drive.google.com/file/d/0B8A0oQ8zbyz5Tm5aYU9HNzlfXzA/view?usp=sharing
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 :
https://drive.google.com/file/d/0B8A0oQ8zbyz5N09UQllZUmNiQlE/view?usp=sharing
Code Construct2
https://drive.google.com/open?id=0B8A0oQ8zbyz5clUybUZEb3IxdGc
B
3
Posts: 4
Reputation: 183

Post » Fri Apr 21, 2017 9:00 pm

It'is Good I have use a solution korbaach.

Thanks you for help
B
3
Posts: 4
Reputation: 183

Post » Sat Apr 22, 2017 10:08 am

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


Array.json :
Code: Select all

"{""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]]
]}"



to
True Json :

Code: Select all
[
   {
      "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
   }
]
B
3
Posts: 4
Reputation: 183


Return to How do I....?

Who is online

Users browsing this forum: blissfulwizard and 11 guests