Dhouard
Dhouard

Reputation: 175

Cannot get array from JSON using getJSONArray

I'm developing an android application which takes a json and gets an array of channels to streming from. Json is generated by this very simple php script.

<?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);

This script send a, I think, correct json string

{
    "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"
    }
}

I load the json and try to get and array with this snippet

    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);
    }

But function getJSONArray throws the error

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

I've been trying to change my original php array to be able to parse the json string but with no luck.

What am I doing wrong?

Thanks in advance.

Upvotes: 0

Views: 432

Answers (4)

amanmehara
amanmehara

Reputation: 134

"channels" here is a json object and not a json array.

You can do the following:

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

Then iterate over the "channelsMap".

For each entry in the JSONObject "channelsMap":

  • key is channel name

  • value is channel url

Upvotes: 0

Siddharth Garg
Siddharth Garg

Reputation: 1570

A JSONObject is something surrounded by {...}. A JSONArray is something surrounded by [....]. So {"key": "value", "key2": "value2"} is a JSONObject with 2 keys whereas [1, 2, 3] &

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

are JSONArray. The second JSONArray is very interesting and says a lot. It is an array of 3 JSONObjects. the key and value can follow any convention you like.

Your script is sending a JSONObject with a key "channels" and a value which is another JSONObject. In java, you are parsing the channels object correctly from response. But you are trying to get a JSONArray from channels which is incorrect.

Either update your script output (Notice the []) :

{
    "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"
    }]
}

Or update your java parsing

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

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

Upvotes: 2

M. Eriksson
M. Eriksson

Reputation: 13635

Looking at your code, it looks like you want a list of objects with name and url (which actually makes more sense).

Change the PHP to match that format:

$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);

This would give you json looking like this:

{
    "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
    ]
}

As others have pointed out, as soon as you have "key" => "value", it will no longer be a json array, but a json object.

Upvotes: 3

Pavneet_Singh
Pavneet_Singh

Reputation: 37404

channels is JSONObject ("channels":{...}) not JSONOArray ([]) so

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

Update : to get json array use a channel index and array() function

<?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);

and java code should be

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
}

Upvotes: 3

Related Questions