如何使用 SparkAR 网络模块获取 JSON

2023-12-10

我想使用 SparkAR 的网络模块从 URL 获取数据 并显示它。

我尝试了 Spark AR 文档中的示例,但效果不大:https://developers.facebook.com/docs/ar-studio/reference/classes/networkingmodule/

不要忘记添加“jsonplaceholder.typicode.com” 首先到 Spark AR 的白名单域。 :)

// Load in the required modules
const Diagnostics = require('Diagnostics');
const Networking = require('Networking');

//==============================================================================
// Create the request
//==============================================================================

// Store the URL we're sending the request to
const url = 'https://jsonplaceholder.typicode.com/posts';

// Create a request object
const request = {

  // The HTTP Method of the request
  // (https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods)
  method: 'POST',

  // The HTTP Headers of the request
  // (https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers)
  headers: {'Content-type': 'application/json; charset=UTF-8'},

  // The data to send, in string format
  body: JSON.stringify({title: 'Networking Module'})

};

//==============================================================================
// Send the request and log the results
//==============================================================================

// Send the request to the url
Networking.fetch(url, request).then(function(result) {

  // Check the status of the result
  // (https://developer.mozilla.org/en-US/docs/Web/HTTP/Status)
  if ((result.status >= 200) && (result.status < 300)) {

    // If the request was successful, chain the JSON forward
    return result.json();

  }

  // If the request was not successful, throw an error
  throw new Error('HTTP status code - ' + result.status);

}).then(function(json) {

  // Log the JSON obtained by the successful request
  Diagnostics.log('Successfully sent - ' + json.title);

}).catch(function(error) {

  // Log any errors that may have happened with the request
  Diagnostics.log('Error - ' + error.message);

});

我得到的只是:“>>成功发送 - 网络 模块”

有谁知道如何让 json 内容显示在控制台中,我想存储它并随后在文本对象中使用它。


就我而言,有一个 url,它给我一个 json 格式的随机项目。

URL: https://gabby-airbus.glitch.me/random

结果:{"item":"项目 14"}

在代码中,替换行:

 Diagnostics.log('Successfully sent - ' + json.title);  

有了这个:

// show json data in console
Diagnostics.log(json.item);

// Asign json data to text object
itemText.text = json.item;

第一行显示控制台中的 json 数据。 第二行将 json 数据分配给场景中的文本对象,在本例中为“item Text”文本对象。

完整代码:

const Diagnostics = require('Diagnostics');
const Scene = require('Scene');
const Networking = require('Networking');
const URL = 'https://gabby-airbus.glitch.me/random';
var itemText = Scene.root.find('itemText');

Networking.fetch(URL).then(function(result){
    if( (result.status >=200) && (result.status < 300)){ return result.json(); } 
    else { throw new Error('HTTP Status Code: ' + result.status); }
}).then(function(json){
    // show json data in console
    Diagnostics.log(json.item);

    // Asign json data to text object
    itemText.text = json.item;
}).catch(function(error){ 
    itemText = 'Failed to start'; 
    Diagnostics.log(result.status + error); 
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 SparkAR 网络模块获取 JSON 的相关文章

  • Azure 数据工厂 - 从 Data Lake Gen 2 JSON 文件中提取信息

    我有一个 ADF 管道 将原始日志数据作为 JSON 文件加载到 Data Lake Gen 2 容器中 我们现在想要从这些 JSON 文件中提取信息 我正在尝试找到从所述文件中获取信息的最佳方法 我发现 Azure Data Lake A
  • nlohmann/json 框架中的未知断点

    一段时间以来 当我在启用断点的 Xcode 下运行我的应用程序时 我显然在名为 nlohmann basic json 的系统框架中遇到了一个未知断点 我可以毫无问题地继续执行 但这无论如何都很烦人 知道这个断点的原因是什么 或者更好的是
  • Ajax 调用诸如 'for (;;); 之类的响应是什么? { json 数据 }' 是什么意思? [复制]

    这个问题在这里已经有答案了 可能的重复 为什么人们会写这样的代码 throw 1 和 for 在 json 响应前面 https stackoverflow com questions 3146798 why do people put c
  • 使用 PHP 从 Mongo 解码 JSON

    我已经看过这个线程 PHP 解码嵌套 JSON https stackoverflow com questions 3555335 php decode nested json并没有设法用它来解决我的问题 我目前正在从 Mongo 获取 J
  • jQuery:处理 getJSON() 中的错误?

    使用 jQuery 时如何处理 500 错误getJSON http api jquery com jQuery getJSON 有几个关于错误处理的问题getJSON and https stackoverflow com questio
  • 使用 VB/ASP classic 可以向 API 发出 HTTP 请求吗?

    是否可以使用 ASP classic 向 Web API 发出请求 例如 像 Flickr API 这样简单的东西 或者这种东西很久以前就不支持了 这是很有可能的 Dim req Set req Server CreateObject MS
  • 将 JSON 文件读入 Spark 时出现 _corrupt_record 错误

    我有这个 JSON 文件 a 1 b 2 这是通过Python json dump方法获得的 现在 我想使用 pyspark 将此文件读入 Spark 中的 DataFrame 根据文档 我正在这样做 sc SparkContext sql
  • Anchor Cycler / 下拉菜单定期导入学校班级数据

    SO 我最近一直在研究一些 html javascript css 为我的学生创建一个在线表格来查看详细信息 分数和各种信息 但我遇到了困难 不确定如何做我正在尝试的事情去做 我将所有这些内容发布在一个线程中的原因是因为我认为其中几个可能会
  • 使用 Python 将 Json 转换为换行 Json 标准

    我有一个获取嵌套对象并删除所有嵌套的代码 使对象平坦 def flatten json y param y Unflated Json return Flated Json out def flatten x name if type x
  • VSTS部署IIS应用程序winrm并更改appsettings.json

    我正在使用 部署 IIS 应用程序 winrm 任务在另一台计算机上部署 IIS 应用程序 此任务部署 zip 文件 在此 zip 中有一个 appsettings json 其变量以下划线开头和结尾 我需要替换每个环境的 appsetti
  • 如何阅读网站内容?

    我是使用 python 2 7 的网络爬虫的新手 一 背景 现在 我想收集有用的数据AQICN org http aqicn org city shenyang usconsulate 这是一个很棒的网站 提供世界各地的空气质量数据 我想用
  • Emoji 字符无法编码为 JSON

    我有一个UITextView我称之为messageField 其中的数据messageField is POST ed 以 JSON 格式发送到服务器 当用户输入表情符号字符时 我无法将数据编码为JSON 我认为 Emoji 使用 Unic
  • 将 JSON URL 转换为 R 数据帧

    我在将 JSON 文件 从 API 转换为 R 中的数据帧时遇到问题 例如 URL 我尝试了 S O 的一些不同建议 包括将json数据转换为R中的数据框 https stackoverflow com questions 28683769
  • GET 数据是否也在 HTTPS 中加密?

    当你拿到时 https crypted google com search q s https encrypted google com search q s Is the s查询已加密 还是只是回应 如果不是 为什么谷歌还要对其公共内容进
  • 双向链表转 JSON

    我有一个三维结构 实际上是一个具有六个节点的双向链表 即左 右 上 下 进 出 如果一个节点位于另一个节点的右侧 那么该节点将毫无疑问位于第一个节点的左侧 喜欢 实际上这是一个 3D 结构 但为了便于理解 我给出了一个 2D 示例 现在我必
  • 如何在 Django REST Framework 中序列化“对象列表”

    我需要一个序列化器来完成这样的事情 items 12 name item 1 66 name item 2 我应该如何声明我的序列化器才能得到这样的东西 这是否是一个有效的 JSON 还是应该如下所示 items name item 1 i
  • 如何在谷歌地图android上显示多个标记

    我想在谷歌地图android上显示带有多个标记的位置 问题是当我运行我的应用程序时 它只显示一个位置 标记 这是我的代码 public class koordinatTask extends AsyncTask
  • 如何通过SQL查询检查是否有JSON函数?

    有SQL 2016 中的 JSON 函数 https learn microsoft com en us sql t sql functions json functions transact sql例如 JSON VALUE JSON Q
  • 创建一个 JSON 对象以在 Spring Boot 测试中发布

    我想编写基本测试来使用 JSON 负载在 users URL 上执行 POST 请求来创建用户 我找不到如何将新对象转换为 JSON 到目前为止有这么多 这显然是错误的 但解释了目的 Test public void createUser
  • AFNetworking 2.0:收到 302 到 AWS S3 时 NSURLSessionDownloadTask 被取消

    我使用 README 文件 来自 AFNetworking 2 0 页面 中的一个简单示例来下载图像 对于直接从我的网络服务器提供的图像来说 一切正常 但对于位于 S3 上的网络服务器 Web 服务器会返回 302 重定向以下错误 Erro

随机推荐