首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JSON在方括号外丢失

JSON在方括号外丢失
EN

Stack Overflow用户
提问于 2014-06-17 03:22:12
回答 1查看 1.6K关注 0票数 0

我需要在数组中包含JSON对象,如:

代码语言:javascript
复制
[{"term":"hemisected","description":"Cut into two equal parts; to bisect, especially along a medial longitudinal plane."},{"term":"polyuria","description":"A condition usually defined as excessive or abnormally large production or passage of urine."},{"term":"dyspnoea","description":"Shortness of breath."}]

但是,下面是将JSON输出为单独的对象:

代码语言:javascript
复制
while ($row = mysql_fetch_array($result)) { 
        $data = array(
        'term' => $row['term'],
        'description' => $row['definition']
        );
echo json_encode($data);
}

比如:

代码语言:javascript
复制
{"term":"hemisected","description":"Cut into two equal parts; to bisect, especially along a medial longitudinal plane."}{"term":"polyuria","description":"A condition usually defined as excessive or abnormally large production or passage of urine."}{"term":"dyspnoea","description":"Shortness of breath."} 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-06-17 03:30:14

目前,json结构没有很好地构建,因为它在循环期间被调用。必须在构建数组(在本例中为$data)之后调用它。考虑一下这个例子:

代码语言:javascript
复制
$data = array();
while ($row = mysql_fetch_array($result)) { 
    $data[] = array(
        'term' => $row['term'],
        'description' => $row['definition'],
    );
}

echo json_encode($data);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24255222

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档