Я получаю 10 записей одновременно из моей базы данных в формате строки Json
PHP-код
$STH = $conn->query('SELECT first, last, email, id from contacts LIMIT 10');
$STH->setFetchMode(PDO::FETCH_ASSOC);
foreach($STH->fetchAll() as $k) {
echo json_encode($k) . "\n";
}
Данные выглядят так, если я отлаживаю:
Debug.Log(www.text);
{"first":"John","last":"Doe","email":"[email protected]","id":"1"}
{"first":"John","last":"malaklsgjakgjka","email":"[email protected]","id":"2"}
{"first":"John","last":"adsjfaksjdaksdj","email":"[email protected]","id":"4"}
{"first":"John","last":"Doe","email":"[email protected]","id":"5"}
{"first":"John","last":"Doe","email":"[email protected]","id":"6"}
{"first":"John","last":"Doe","email":"[email protected]","id":"7"}
{"first":"John","last":"Doe","email":"[email protected]","id":"8"}
{"first":"dolly","last":"Doe","email":"[email protected]","id":"9"}
{"first":"molly","last":"Doe","email":"[email protected]","id":"10"}
{"first":"John","last":"Doe","email":"[email protected]","id":"11"}
Теперь я хочу преобразовать каждую запись в строку и собрать список / массив строк
В настоящее время я делаю следующее:
JSONNode arr = JSONNode.Parse(www.text);
for(int i =0 ; i < arr.Count ; i++){
string str = string.Empty;
//str += arr[i]["first"] + "-";
//str += arr[i]["last"] + "-";
//str += arr[i]["id"] + "-";
//str += arr[i]["email"];
Debug.Log(arr[i]["first"]);
//myList.add(str);
}
но если я отлаживаю его, я получаю значения NULL. Что именно здесь не так?
Вывод должен выглядеть так:
Debug.log(myList[i]);
"John- Doe - [email protected] - 1""John- malaklsgjakgjka - [email protected]""John- adsjfaksjdaksdj - [email protected] - 4""John- Doe - [email protected] - 5"etc.
Сначала взглянем на обозначение json:
<?php
$json = '[{"first":"John","last":"Doe","email":"[email protected]","id":"1"},
{"first":"John","last":"malaklsgjakgjka","email":"[email protected]","id":"2"},
{"first":"John","last":"adsjfaksjdaksdj","email":"[email protected]","id":"4"},
{"first":"John","last":"Doe","email":"[email protected]","id":"5"},
{"first":"John","last":"Doe","email":"[email protected]","id":"6"},
{"first":"John","last":"Doe","email":"[email protected]","id":"7"},
{"first":"John","last":"Doe","email":"[email protected]","id":"8"},
{"first":"dolly","last":"Doe","email":"[email protected]","id":"9"},
{"first":"molly","last":"Doe","email":"[email protected]","id":"10"},
{"first":"John","last":"Doe","email":"[email protected]","id":"11"}]';
$array = json_decode($json, true);
for($i = 0; $i < count($array); $i++) {
echo implode(' - ', $array[$i]) . "<br>\n";
}
?>
Измените свой код на это:
$STH = $conn->query('SELECT first, last, email, id from contacts LIMIT 10');
$STH->setFetchMode(PDO::FETCH_ASSOC);
$json = array();
foreach($STH->fetchAll() as $k) {
$json[] = json_encode($k);
}
echo '[' . implode(',', $json) . ']';
?>
static void WebCall () {
string response = "[{\"name\":\"South Africa\",\"client_access_key\":\"0b3491e59b2e3d28909001b1178066d3cf2bf5cc\",\"client_secret_key\":\"e76eacac21f0dc36ba3aab37f2e8a7196cebe1bf\",\"id\":66749,\"image\":\"http://assets.viewa.com/media/2935619/sa.png\",\"autoDetect\":false,\"countryCode\":\"ZA\"},{\"name\":\"USA\",\"client_access_key\":\"0b3491e59b2e3d28909001b1178066d3cf2bf5cc\",\"client_secret_key\":\"e76eacac21f0dc36ba3aab37f2e8a7196cebe1bf\",\"id\":151957,\"image\":\"http://assets.viewa.com/media/2306953/us.png\",\"autoDetect\":false,\"countryCode\":\"US\"},{\"name\":\"India\",\"client_access_key\":\"0b3491e59b2e3d28909001b1178066d3cf2bf5cc\",\"client_secret_key\":\"e76eacac21f0dc36ba3aab37f2e8a7196cebe1bf\",\"id\":112705,\"image\":\"http://assets.viewa.com/media/2306943/indian.jpg\",\"autoDetect\":true,\"countryCode\":\"IN\"},{\"name\":\"Australia\",\"client_access_key\":\"0b3491e59b2e3d28909001b1178066d3cf2bf5cc\",\"client_secret_key\":\"e76eacac21f0dc36ba3aab37f2e8a7196cebe1bf\",\"id\":53693,\"image\":\"http://assets.viewa.com/media/2306928/australia.png\",\"autoDetect\":false,\"countryCode\":\"AU\"}]";
Debug.Log ("Response :" + response);
JSONNode test = JSONNode.Parse (response);
int count = test.Childs.Count ();
List<Regions> regionList = new List<Regions>();
for (int i = 0; i<test.Childs.Count(); i++) {
Regions region = new Regions();
region.name = test[i]["name"].Value;
region.client_access_key= test [i] ["client_access_key"].Value;
region.client_secret_key = test [i] ["client_secret_key"].Value;
region.id = test [i] ["id"].AsInt;
region.image = test [i] ["image"];
region.autoDetect = test [i] ["autoDetect"].AsBool;
region.countryCode = test [i] ["countryCode"];
regionList.Add(region);
}
}
[System.Serializable]
Районы общественного класса {
public string name;
public string client_access_key;
public string client_secret_key;
public long id;
public string image;
public bool autoDetect;
public string countryCode;
public string SaveToJsonString()
{
return JsonUtility.ToJson(this);
}
}
Это сработало для меня.
$sql = 'SELECT first, last, email, id from contacts LIMIT 10';
$result = mysqli_query($conn, $sql);
$json = array();
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
$json[]= array(
'first' => $row['first'],
'last' => $row['last'],
'email' =>$row['email'],
'id' => $row['id']
);
}
$jsonstring = json_encode($json);
echo $jsonstring;