使用 SimpleJson 将 JSON 数据转换为 unity3d 中的数组

Converting JSON data into an array in unity3d using SimpleJson

我一次从我的数据库中以 Json 字符串格式

获取 10 条记录

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":"john@example.com","id":"1"}
{"first":"John","last":"malaklsgjakgjka","email":"john@example.com","id":"2"}
{"first":"John","last":"adsjfaksjdaksdj","email":"john@example.com","id":"4"}
{"first":"John","last":"Doe","email":"john@example.com","id":"5"}
{"first":"John","last":"Doe","email":"john@example.com","id":"6"}
{"first":"John","last":"Doe","email":"john@example.com","id":"7"}
{"first":"John","last":"Doe","email":"john@example.com","id":"8"}
{"first":"dolly","last":"Doe","email":"john@example.com","id":"9"}
{"first":"molly","last":"Doe","email":"john@example.com","id":"10"}
{"first":"John","last":"Doe","email":"john@example.com","id":"11"}

现在我想将每条记录转换成一个字符串并组合成 list/array 个字符串

目前我正在做以下事情:

          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 - john@example.com - 1"
"John- malaklsgjakgjka - john@example.com-2"
"John- adsjfaksjdaksdj - john@example.com - 4"
"John- Doe - john@example.com - 5" 
 etc.

首先,看一下 json 表示法:

<?php

$json = '[{"first":"John","last":"Doe","email":"john@example.com","id":"1"},
{"first":"John","last":"malaklsgjakgjka","email":"john@example.com","id":"2"},
{"first":"John","last":"adsjfaksjdaksdj","email":"john@example.com","id":"4"},
{"first":"John","last":"Doe","email":"john@example.com","id":"5"},
{"first":"John","last":"Doe","email":"john@example.com","id":"6"},
{"first":"John","last":"Doe","email":"john@example.com","id":"7"},
{"first":"John","last":"Doe","email":"john@example.com","id":"8"},
{"first":"dolly","last":"Doe","email":"john@example.com","id":"9"},
{"first":"molly","last":"Doe","email":"john@example.com","id":"10"},
{"first":"John","last":"Doe","email":"john@example.com","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) . ']';

?>

这对我有用。

$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;
  1. 这是我们转换为数组的Json

[{"name":"South Africa","client_access_key":"78066d3cf2bf5cc","client_secret_key":"7f2e8a7196cebe1bf","id":66749,"image":"a.png","autoDetect":false,"countryCode":"ZA"},{"name":"USA","client_access_key":" 0b3491d3cf2bf5cc","client_secret_key":"e76eac8a71","id":151957,"image":"b.png","autoDetect":false,"countryCode":"US"},{"name":"India","client_access_key":"0b378066d3cf2bf5cc","client_secret_key":"e76eaca8a7196cebe1bf","id":112705,"image":"c.jpg","autoDetect":true,"countryCode":"IN"},{"name":"Australia","client_access_key" :"0b3491e59b2d3cf","client_secret_key":"e76eacac21f","id":53693,"image":"d.png","autoDetect":false,"countryCode" :"AU"}]

  1. 在网络回调函数中,我们将响应 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 class 地区 {

    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);
    }

}