将Firebase数据库数据从字符串转换为数组

时间:2019-06-08 16:06:49

标签: javascript arrays json firebase for-loop

我正在尝试使用Xenioo平台构建聊天机器人,并且其“列表”功能返回编译错误。绕过我要从Firebase获取数据的JavaScript变量:

        { documents:[{
  "documents": [
    {
      "name": "projects/*******/databases/(default)/documents/Assets/QWEWQXLUyOl8vQXypHo7",
      "fields": {
        "Lawyer Phone": {
          "stringValue": "6"
        },
        "Sale Type": {
          "stringValue": "7"
        },
        "Area": {
          "stringValue": "2"
        },
        "Address": {
          "stringValue": "1"
        },
        "Lawyer Name": {
          "stringValue": "5"
        },
        "District": {
          "stringValue": "4"
        },
        "Asset Type": {
          "stringValue": "3"
        },
        "Additional": {
          "stringValue": "0"
        }
      },
      "createTime": "2019-06-06T17:42:30.592904Z",
      "updateTime": "2019-06-08T15:52:36.649013Z"
    },
    {
      "name": "projects/*******/databases/(default)/documents/Assets/fzdbqZFet4GskHqVTGaT",
      "fields": {
        "Additional": {
          "stringValue": "a"
        },
        "Lawyer Phone": {
          "stringValue": "g"
        },
        "Sale Type": {
          "stringValue": "h"
        },
        "Area": {
          "stringValue": "c"
        },
        "Address": {
          "stringValue": "b"
        },
        "Lawyer Name": {
          "stringValue": "f"
        },
        "District": {
          "stringValue": "e"
        },
        "Asset Type": {
          "stringValue": "d"
        }
      },
      "createTime": "2019-06-08T15:50:38.053127Z",
      "updateTime": "2019-06-08T15:52:27.389321Z"
    }
  ]
}
]}

我希望能够将这些数据作为普通数组或某些我可以根据用户要求简单地循环和过滤数据的东西处理,例如:

var data = "";

for( var i=0; i < result.length; i++ ){
        data += result[ i ].Additional + "\n";
}

我使用的语言是JavaScript,我是Noby,非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我相信要做的第一件事就是将变量转换为对象实例。

IEnumerator goDown()
{
    yield return new WaitForSeconds(62);

    Vector3 goal = new Vector3(-1.4f, -1.3f, 9.33f);

    while (transform.position != goal) 
    {
        transform.position = Vector3.MoveTowards(transform.position, goal, 
                                                 1.03f * Time.deltaTime);
        yield return null;
    }

    yield return new WaitForSeconds(25);

    goal = new Vector3(-8.79f, 1.67f, 9.86f);

    while (transform.position != goal) 
    {
        transform.position = Vector3.MoveTowards(transform.position, goal, 
                                                 1.03f * Time.deltaTime);
        yield return null;
    }
}

在那之后,您几乎可以使用该变量做任何您想做的事,例如:

var value = conversation.GetVariableValue( "my_firebase_data" );
var instance = JSON.parse( value )

我还认为Xenioo支持聊天机器人中的直接变量解析,例如“ Hello {{data [0] .first_name}}”等。