JSON编码在PHP中给了我无效的JSON文件-从MYSQL生成

我正在尝试生成一个 JSON 文件以供统一游戏读取。我使用 C# 调用 PHP 文件,在其中使用 MYSQL 查询从我的数据库中获取相关信息。我得到了我想要的内容,但是当我编码为 JSON 文件时,它是不可读的。我可以看到发生了什么,但我不知道如何解决!编码发生时,每条记录后我都会得到 }{ 。这是我的 php 文件中的相关代码(我认为)


$sqlnew = "SELECT DISTINCT

   content.ContentTitle,

   content.ContentBody,

   user_habits.value,

  user_habits.boolean,

  users.User_ID


FROM user_habits

       INNER JOIN users

         ON user_habits.user_id = users.User_ID,

     content_habitparameters

       INNER JOIN content

         ON content_habitparameters.Content_ID = content.Content_ID

WHERE users.User_ID = 1

AND ((user_habits.value >= content_habitparameters.Min_Value

and user_habits.value <= content_habitparameters.Max_Value)

OR user_habits.boolean = content_habitparameters.Boolean)";


$result = $conn->query($sqlnew);


if ($result->num_rows > 0) 

{

   $dbdata = array();

}

while ($row = $result->fetch_assoc())

{

$dbdata[]=$row;

}


//print array in JSON format

echo json_encode($dbdata);

我的 SQL 结果如下所示:


ContentTitle:平均每日香烟 - ContentBody:吸烟会在例如打电话和吸烟之间建立联系。你需要打破这些习惯才能成功。请注意您何时倾向于获得同性恋并为这些情况考虑替代方案。- value: 30 - user_habits.boolean: - users.User_ID: 1 users.Username lz7cjc


ContentTitle:克服一个大诱惑,避免使用许多较小的诱惑 - ContentBody:通过从你的房子、汽车、工作和任何你花时间的地方移除所有与吸烟有关的物品,给自己最大的成功机会。避免购买烟草产品,这样会更难屈服于诱惑。行为改变不仅仅是意志力。这也是关于停止或开始做某事的难易程度。让吸烟更难,更容易抗拒诱惑 - 价值: - 用户


当我编码成 JSON 文件时,我得到了这个:


[  {"ContentTitle":"Average Daily Cigarettes",

    "ContentBody":"Smoking cigarettes creates a link between e.g. Being on the phone and smoking. You need to break these habits to succeed. Be aware of when you tend to reach for a fag and think of alternatives for those situations.",

    "value":"30",

    "boolean":null,

    "User_ID":"1",

    "Username":"lz7cjc"

   },{

    "ContentTitle":"Fight one big temptation to avoid lots of smaller ones",

    "value":null,

    "boolean":"1",

    "User_ID":"1",

    "Username":"lz7cjc"

  },{


显然我无法统一阅读。我想解决的一种方法是创建多个 JSON 文件,但这对我来说也很笨重。


非常感谢帮助。谢谢


四季花海
浏览 93回答 3
3回答

凤凰求蛊

好吧,您的 json_encoding 数组可以称为列表,其中的数组可以称为地图。例如,您将这样的数组传递给函数:array( // this is a List, you have only object&nbsp;array( // this is a Map, you have key value pairs&nbsp; 'Content-Title' => ...,&nbsp; 'ContentBody'&nbsp; &nbsp;=> ...,&nbsp;),&nbsp;array( // this is a Map, you have key value pairs&nbsp; 'Content-Title'=>...,&nbsp; 'ContentBody'&nbsp; &nbsp;=> ...,&nbsp;),)编码这会在结尾添加一个 [ ] 以表明它是一个列表。[{...}{...}]如果您想要一个 map / json 对象,您必须在添加结果时指定键并在 C# 脚本中调用这些键。$count = 1;while ($row = $result->fetch_assoc()){&nbsp;$dbdata["result-$count"] = $row;&nbsp;$count++;}这反过来会给你一个数组,如:array( // this is a Map, you have key value pairs'result-1' => array( // this is a Map, you have key value pairs&nbsp; 'Content-Title' => ...,&nbsp; 'ContentBody'&nbsp; &nbsp;=> ...,&nbsp;),'result-2' => array( // this is a Map, you have key value pairs&nbsp; 'Content-Title'=>...,&nbsp; 'ContentBody'&nbsp; &nbsp;=> ...,&nbsp;),)这应该导致如下编码:{"result-1":{...},"result-2":{...}}编辑:某些语言将地图和列表作为单独的数据类型 C# 将它们分开。而在 PHP 中,数组具有两者的功能。顾名思义,映射通常是一种结构,它在用户指定的键的帮助下映射出值。$map = array(&nbsp;'this_key'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'will_give_me_this_result',&nbsp;'and_this_one'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;=> 'will_give_me_value_at_this_key',&nbsp;'I_help_you_find_whats_on_the_right' => ' \0/" ',);虽然列表不需要使用键来指向值。您可以使用索引 0,1.. 它们具有键的功能,但仍然不会被视为键,因为它们指定了值的位置并且不需要显式设置。$list = array(&nbsp;'IM at position 0',&nbsp;"and Im at 1, but hey you don't need me to tell you this, I'll always be here",&nbsp;"position 2, well unless you manipulate me bastard",&nbsp;array(&nbsp; 'let me be clear',&nbsp; 'my parent is still just a value of outer most array',&nbsp; "you still now the position of it"&nbsp;));现在,如果我们 json_encoded 这两个数组:数组/$地图{"this_key":"will_give_me_this_result","and_this_one":"will_give_me_value_at_this_key","I_help_you_find_whats_on_the_right":" \\0\/\" "}数组/$列表["IM at position 0","and Im at 1, but hey you don't need me to tell you this, I'll always be here","position 2, well unless you manipulate me bastard",["let me be clear","my parent is still just a value of the outer most array","you still now the position of it"]]注意到不同的括号了吗?[ ] 表示里面的对象按可预测的 0,1,2 顺序排列,它们不需要键来指定它们。另一方面 { } 指定一个地图。您可以看到有多个 "key_key" : "value_value" 出现。那是因为您需要密钥才能知道什么是什么值。现在,我个人不知道 C# 以及如何用该特定语言解码 json,所以我预感你想要获得某种 key:value 结果,但你不能,因为你的 json 输出表明 List 不是地图。这是 json_encoding 中的数组,但为可见性添加了空格。数组/$地图{&nbsp;"this_key":"will_give_me_this_result",&nbsp;"and_this_one":"will_give_me_value_at_this_key",&nbsp;"I_help_you_find_whats_on_the_right":" \\0\/\" "}数组/$列表[&nbsp; "IM at position 0",&nbsp; "and Im at 1, but hey you don't need me to tell you this, I'll always be here",&nbsp; "position 2, well unless you manipulate me bastard",&nbsp;[&nbsp; &nbsp;"let me be clear",&nbsp; &nbsp;"my parent is still just a value of the outer most array",&nbsp; &nbsp;"you still now the position of it"&nbsp;]]

慕尼黑的夜晚无繁华

如果您使用的是 PDO,请尝试:$dbdata = $result->fetchAll(PDO::FETCH_ASSOC); echo json_encode($dbdata);这会将结果作为 php 数组获取。那不应该创建一个无效的 json。

斯蒂芬大帝

所以不是 JSON 文件/XML,而是 Unity 无法处理数据。感谢所有指出这一点的人。这是已成功实施的截断视图:{PlayerTipsJSON loadedPlayerData = JsonUtility.FromJson<PlayerTipsJSON>(json);for (int i = 0;i<loadedPlayerData.data.Count; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log(loadedPlayerData.data[i].User_ID + "\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log(loadedPlayerData.data[i].ContentTitle + "\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log(loadedPlayerData.data[i].ContentBody+ "\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log(loadedPlayerData.data[i].value + "\n");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Debug.Log(loadedPlayerData.data[i].boolean + "\n");&nbsp; &nbsp; &nbsp; &nbsp; }}[System.Serializable]&nbsp; &nbsp; public class PlayerData&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string fromJSONusername;&nbsp; &nbsp; &nbsp; &nbsp; public string ContentTitle;&nbsp; &nbsp; &nbsp; &nbsp; public string ContentBody;&nbsp; &nbsp; &nbsp; &nbsp; public int User_ID;&nbsp; &nbsp; &nbsp; &nbsp; public int value;&nbsp; &nbsp; &nbsp; &nbsp; public bool boolean;&nbsp; &nbsp; }&nbsp; &nbsp; [System.Serializable]&nbsp; &nbsp; public class PlayerTipsJSON&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public List<PlayerData> data;&nbsp; &nbsp; }我没有评论代码,因为我没有编写它,老实说不是 100% 确定发生了什么!我认为它是一个数组的数组。
打开App,查看更多内容
随时随地看视频慕课网APP