【问题标题】:Formatting php output correctly正确格式化php输出
【发布时间】:2018-01-11 09:56:33
【问题描述】:

我做了一些研究,但我找不到我要找的东西。 我远非 PHP 专家,但我想我正在尝试做一些相当简单的事情。

在 Unity3D 中,我想从 PHP 输出中接收几个值,这些值是我执行发布请求的。

输出来自 arangoDB,如下所示:

array(1) {
  [0]=>
  object(ArangoDBClient\Document)#9 (9) {
    ["_id":protected]=>
    string(16) "Producten/140368"
    ["_key":protected]=>
    string(6) "140368"
    ["_rev":protected]=>
    string(11) "_WMOJhJe--_"
    ["_values":protected]=>
    array(3) {
      ["Naam Product"]=>
      string(9) "Naam-Foo2"
      ["Categorie Product"]=>
      string(14) "Categorie-Foo2"
      ["Discipline Product"]=>
      string(15) "Discipline-Foo2"
    }
    ["_changed":protected]=>
    bool(true)
    ["_isNew":protected]=>
    bool(false)
    ["_doValidate":protected]=>
    bool(false)
    ["_hiddenAttributes":protected]=>
    array(0) {
    }
    ["_ignoreHiddenAttributes":protected]=>
    bool(false)
  }
}

我只对这部分感兴趣:

["Naam Product"]=>
      string(9) "Naam-Foo2"
      ["Categorie Product"]=>
      string(14) "Categorie-Foo2"
      ["Discipline Product"]=>
      string(15) "Discipline-Foo2"

最好的情况是这样格式化的:

Naam Product: Naam-Foo2;
Categorie Product: Categorie-Foo2;
Discipline Product: Discipline-Foo2;

如何过滤这些信息,以便在 Unity 中读取时获得干净的数据字符串?

对此有何建议? Thnx 进阶了!

【问题讨论】:

    标签: php arrays string unity3d arangodb


    【解决方案1】:

    就我而言,最好将JSON 数据从服务器发送到客户端。只需将 Newtonsoft.Json 库添加到您的 Unity3D 项目中。然后,您可以轻松地将收到的 json 字符串反序列化为 C# 对象。这是一个简单的例子:

    string jsonString = "Your json string";
    
    var deserializedObject = Newtonsoft.Json.JsonConvert.DeserializeObject<YOUR_POCO_OBJECT>(jsonString);
    

    现在您可以使用deserializedObject 字段


    这是一个简单的 C# 控制台应用程序示例

    namespace JsonDEMO
    {
        public class Product
        {
            public string Name { get; set; }
            public string Category { get; set; }
            public string Discipline { get; set; }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                string jsonString = "{ 'Name': 'Naam-Foo2','Category': 'Categorie-Foo2','Discipline': 'Discipline-Foo2'}";
    
                var deserializedObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Product>(jsonString);
    
                System.Console.WriteLine(deserializedObject.Name);
                System.Console.WriteLine(deserializedObject.Category);
                System.Console.WriteLine(deserializedObject.Discipline);
            }
        }
    }
    

    【讨论】:

    • 是的,我认为你是对的。我已经读过它,但我想在我可以从我的 php 发送一个 json 字符串之前,我必须将它清理一下我需要的点点滴滴?因为现在我有 1 个简单的数组,我可以将其转换为 json,然后统一接收为 json,我假设。至少这就是我现在想要做的:) 我添加了 echo json_encode($interested_array);现在我有干净的 json 作为输出,感谢您的帮助
    【解决方案2】:

    尝试使用getAll 方法:

    $interested_array = $filter[0]->getAll();
    print_r($interested_array);
    

    【讨论】:

    • 它给了我这个错误:致命错误:未捕获错误:不能使用 ArangoDBClient\Cursor 类型的对象作为 C:\xampp\htdocs\ArangoTest\index.php:62 中的数组 堆栈跟踪:#0 {main} 在第 62 行的 C:\xampp\htdocs\ArangoTest\index.php 中抛出,我删除了 [],现在它的输出已经更清晰了!它似乎是一个数组中的一个数组。你是怎么过滤的?
    猜你喜欢
    • 2014-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-03
    相关资源
    最近更新 更多