无法使用getJSONArray

时间:2017-09-15 16:08:32

标签: php android arrays json

我正在开发一个Android应用程序,它接受一个json并获取一系列通道来实现。 Json是由这个非常简单的PHP脚本生成的。

<?php

$channels = [
'channels' => [
        'tele5' => 'http://mset-prod-1.live-delivery.ooyala.com/out/u/3vkkbgnvsm2r5/104951/RsZXlnNTE6re1fFuFEI0UrdA2Uj7pNGM/cn/5d62490854b545598f64eaa84709400b_4.m3u8',
        'divinity' => 'http://mset-prgb-2.live-delivery.ooyala.com/out/u/3vkkbgnvsm2r5/104951/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B/cn/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8',
        'bemad' => 'http://mset-prgb-2.live-delivery.ooyala.com/out/u/3vkkbgnvsm2r5/104951/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B/cn/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8',
        'antena3' => 'http://a3live-lh.akamaihd.net/i/antena3_1@35248/master.m3u8'
        ]
];

echo json_encode($channels);

这个脚本发送一个我认为正确的json字符串

{
    "channels":{
        "tele5":"http:\/\/mset-prod-1.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/RsZXlnNTE6re1fFuFEI0UrdA2Uj7pNGM\/cn\/5d62490854b545598f64eaa84709400b_4.m3u8",
        "divinity":"http:\/\/mset-prgb-2.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B\/cn\/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8",
        "bemad":"http:\/\/mset-prgb-2.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B\/cn\/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8",
        "antena3":"http:\/\/a3live-lh.akamaihd.net\/i\/antena3_1@35248\/master.m3u8"
    }
}

我加载json并尝试使用此代码段获取和数组

    JSONObject json = new JSONObject(response);

    channels = json.getJSONArray("channels");
    for (int i = 0; i < channels.length(); i++) {
        JSONObject channel = channels.getJSONObject(i);
        String name = channel.getString("name");
        String url = channel.getString("url");
        Channel ch = new Channel(name, url);
        result.add(ch);
    }

但是函数getJSONArray会抛出错误

Value {"tele5":"http:\/\/mset-prod-1.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/RsZXlnNTE6re1fFuFEI0UrdA2Uj7pNGM\/cn\/5d62490854b545598f64eaa84709400b_4.m3u8","divinity":"http:\/\/mset-prgb-2.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B\/cn\/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8","bemad":"http:\/\/mset-prgb-2.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B\/cn\/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8","antena3":"http:\/\/a3live-lh.akamaihd.net\/i\/antena3_1@35248\/master.m3u8"} at channels of type org.json.JSONObject cannot be converted to JSONArray

我一直试图改变原来的php数组,以便能够解析json字符串,但没有运气。

我做错了什么?

提前致谢。

4 个答案:

答案 0 :(得分:3)

channelsJSONObject"channels":{...})而非JSONOArray[])所以

JSONObject channels = json.getJSONObject("channels");

更新:使json数组使用channel索引和array()函数

<?php
  $channels['channels'] = array([
        'tele5' => 'http://mset-prod-1.live-delivery.ooyala.com/out/u/3vkkbgnvsm2r5/104951/RsZXlnNTE6re1fFuFEI0UrdA2Uj7pNGM/cn/5d62490854b545598f64eaa84709400b_4.m3u8',
        'divinity' => 'http://mset-prgb-2.live-delivery.ooyala.com/out/u/3vkkbgnvsm2r5/104951/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B/cn/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8',
        'bemad' => 'http://mset-prgb-2.live-delivery.ooyala.com/out/u/3vkkbgnvsm2r5/104951/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B/cn/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8',
        'antena3' => 'http://a3live-lh.akamaihd.net/i/antena3_1@35248/master.m3u8'

]);

echo json_encode($channels);

和java代码应该是

JSONObject json = new JSONObject(response);

channels = json.getJSONArray("channels");
for (int i = 0; i < channels.length(); i++) {
    JSONObject channel = channels.getJSONObject(i);
    String tele5 = channel.getString("tele5");
    String bemad = channel.getString("bemad");
    // use appropriate keys
}

答案 1 :(得分:3)

查看您的代码,看起来您想要一个包含nameurl的对象列表(实际上更有意义)。

更改PHP以匹配该格式:

$channels = [
    'channels' => [
        [
            'name' => 'tele5',
            'url'  => 'http://mset-prod-1.live-delivery.ooyala.com/out/u/3vkkbgnvsm2r5/104951/RsZXlnNTE6re1fFuFEI0UrdA2Uj7pNGM/cn/5d62490854b545598f64eaa84709400b_4.m3u8'
        ],

        [
            'name' => 'divinity',
            'url'  => 'http://mset-prgb-2.live-delivery.ooyala.com/out/u/3vkkbgnvsm2r5/104951/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B/cn/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8'
        ],

        ... and so on
    ]
];

echo json_encode($channels);

这会让你的json看起来像这样:

{
    "channels":[
        {
            "name": "tele5",
            "url": "http:\/\/mset-prod-1.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/RsZXlnNTE6re1fFuFEI0UrdA2Uj7pNGM\/cn\/5d62490854b545598f64eaa84709400b_4.m3u8"
        },
        {
            "name": "divinity",
            "url": "http:\/\/mset-prgb-2.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B\/cn\/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8"
        },

        ... and so on
    ]
}

正如其他人所指出的,只要你有"key" => "value",它就不再是一个json数组,而是一个json对象。

答案 2 :(得分:2)

JSONObject被{...}包围。 JSONArray是由[....]包围的东西。所以{"key": "value", "key2": "value2"}是一个带有2个键的JSONObject [1, 2, 3] &安培;

[{"key1" : "value1-1", "key2": "value2-1"},
 {"key1":  "value1-2", "key2": "value2-2"}, 
 {"key3",  "value3",   "key4": "value4"}]

是JSONArray。 第二个JSONArray非常有趣并且说了很多。它是一个包含3个JSONObjects的数组。关键和价值可以遵循你喜欢的任何惯例。

您的脚本正在发送带有密钥&#34;频道&#34;的JSONObject。和一个值是另一个JSONObject。 在java中,您正在从响应中正确地解析通道对象。但是你试图从不正确的频道获取JSONArray。

更新脚本输出(注意[])

{
    "channels":[{
        "tele5":"http:\/\/mset-prod-1.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/RsZXlnNTE6re1fFuFEI0UrdA2Uj7pNGM\/cn\/5d62490854b545598f64eaa84709400b_4.m3u8",
        "divinity":"http:\/\/mset-prgb-2.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B\/cn\/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8",
        "bemad":"http:\/\/mset-prgb-2.live-delivery.ooyala.com\/out\/u\/3vkkbgnvsm2r5\/104951\/IybDNyYTE6j7PZaKerX9iKFSCuctjf5B\/cn\/95d3fccb2d6f4ad6b09d23e7c67d3acf_4.m3u8",
        "antena3":"http:\/\/a3live-lh.akamaihd.net\/i\/antena3_1@35248\/master.m3u8"
    }]
}

或者更新你的java解析

JSONObject json = new JSONObject(response);
channel = json.getJSONObject("channels");

// Whatever you want to do with this single channel (this is not an array)

答案 3 :(得分:0)

“channels”这里是一个json对象而不是json数组。

您可以执行以下操作:

JSONObject channelsMap = json.getJSONObject(“channels”);

然后遍历“channelsMap”。

对于JSONObject“channelsMap”中的每个条目:

  • 键是频道名称

  • 值为频道网址