因此,我正在使用Ffltter应用程序从网页中拉出JSON.当我将应用程序指向:HTTPS://jsonplaceholder.ypicode.com/post It Works 100%时,输出如下(缩写):

[
  {
    "userId": 1,
    "id": 1,
    "title": "sunt aut facere repellat provident occaecati excepturi optio reprehenderit",
    "body": "quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto"
  },
  {
    "userId": 1,
    "id": 2,
    "title": "qui est esse",
    "body": "est rerum tempore vitae\nsequi sint nihil reprehenderit dolor beatae ea dolores neque\nfugiat blanditiis voluptate porro vel nihil molestiae ut reiciendis\nqui aperiam non debitis possimus qui neque nisi nulla"
  }
]

当我指向我自己的测试站点时,输出如下:

[{"userId":"1","id":"1","title":"TEST for Title","body":"Test for Body"},{"userId":"1","id":"2","title":"TEST for Title","body":"Test for Body"},{"userId":"1","id":"3","title":"TEST for Title","body":"Test for Body"},{"userId":"1","id":"4","title":"TEST for Title","body":"Test for Body"},{"userId":"1","id":"5","title":"TEST for Title","body":"Test for Body"},{"userId":"1","id":"6","title":"TEST for Title","body":"Test for Body"}]

PHP代码如下所示:

 $sql = "select supplier_id as userId,  id, 'TEST for Title' as title, 'Test for Body' as body from sales_orders";
    $result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

  
    $rows = $result->fetch_all(MYSQLI_ASSOC);

    header('Content-type: application/json');
  
    echo json_encode($rows);

我看到的唯一真正的区别是间距布局,整数周围没有"".

在这件事上猛击我的头.

我可以在Flutter 控制台中打印来self 的站点的输出,但它不会填充到我的应用程序中,因为字段名称完全相同,我只能假设是JSON格式造成了我的麻烦.

作为参考,我的省道代码(Flutter )如下(https://www.geeksforgeeks.org/http-get-response-in-flutter/):

import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
    return MaterialApp(
    home: HomePage(),
    );
}
}

//Creating a class user to store the data;
class User {
final int id;
final int userId;
final String title;
final String body;

User({
    this.id,
    this.userId,
    this.title,
    this.body,
});
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
//Applying get request.

Future<List<User>> getRequest() async {
    //replace your restFull API here.
    String url = "https://jsonplaceholder.typicode.com/posts";
    final response = await http.get(url);

    var responseData = json.decode(response.body);

    //Creating a list to store input data;
    List<User> users = [];
    for (var singleUser in responseData) {
    User user = User(
        id: singleUser["id"],
        userId: singleUser["userId"],
        title: singleUser["title"],
        body: singleUser["body"]);

    //Adding user to the list.
    users.add(user);
    }
    return users;
}

@override
Widget build(BuildContext context) {
    return SafeArea(
    child: Scaffold(
        appBar: AppBar(
        title: Text("Http Get Request."),
        leading: Icon(
            Icons.get_app,
        ),
        ),
        body: Container(
        padding: EdgeInsets.all(16.0),
        child: FutureBuilder(
            future: getRequest(),
            builder: (BuildContext ctx, AsyncSnapshot snapshot) {
            if (snapshot.data == null) {
                return Container(
                child: Center(
                    child: CircularProgressIndicator(),
                ),
                );
            } else {
                return ListView.builder(
                itemCount: snapshot.data.length,
                itemBuilder: (ctx, index) => ListTile(
                    title: Text(snapshot.data[index].title),
                    subtitle: Text(snapshot.data[index].body),
                    contentPadding: EdgeInsets.only(bottom: 20.0),
                ),
                );
            }
            },
        ),
        ),
    ),
    );
}
}

推荐答案

您可以使用标志JSON_NUMERIC_CHECK来防止数字引用:

$rows = $result->fetch_all(MYSQLI_ASSOC);
echo json_encode($rows,  JSON_NUMERIC_CHECK);

php code online

Php相关问答推荐

WooCommerce拆分运输包裹上的商品数量增加运输成本

如何在函数中定位WooCommerce产品循环

在没有symfony应用程序的情况下使用安全Bundle 包时,缺少配置构建器类

使用列入黑名单的单词和自定义业务逻辑的组合将特定的子字符串包装在HTML标记中

不包括WooCommerce中单独销售的产品

如何在微软图形API中使用用户S访问令牌或基于租户ID的访问令牌?

从字符串转换日期和/或时间时,Laravel转换失败

在Laravel中创建辅助函数时的约定

HTTPPost请求在从php脚本调用时返回404,但在从node.js脚本调用时有效.终结点有效

添加不存在的订单元数据以扩展WooCommerce管理订单搜索

在 WooCommerce 更新结帐事件上动态更新自定义内容

使用 PHP,我可以将 foreach 语句放入瞬态中吗?

将一个表中的行连接为不同表MYSQL中的一列

PHP Laravel 迁移文件不创建关系

有没有办法获取触发流操作的页面的 URI?

如何显示所有数组数据而不仅仅是最后的结果

图像未在 Laravel Ajax Crud 中显示

Woocommerce注册中的自定义复选框验证错误提示问题

php/html OnSubmit 禁用输入类型提交未给出预期结果

将加密/解密函数从 PHP 转换为 NodeJS