Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7688903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:02:12+00:00 2026-05-31T20:02:12+00:00

I am getting JSON string from website. I have data which looks like this

  • 0

I am getting JSON string from website. I have data which looks like this (JSON Array)

 myconf= {URL:[blah,blah]}

but some times this data can be (JSON object)

 myconf= {URL:{try}}

also it can be empty

 myconf= {}    

I want to do different operations when its object and different when its an array. Till now in my code I was trying to consider only arrays so I am getting following exception. But I am not able to check for objects or arrays.

I am getting following exception

    org.json.JSONException: JSONObject["URL"] is not a JSONArray.

Can anyone suggest how it can be fixed. Here I know that objects and arrays are the instances of the JSON object. But I couldn’t find a function with which I can check whether the given instance is a array or object.

I have tried using this if condition but with no success

if ( myconf.length() == 0 ||myconf.has("URL")!=true||myconf.getJSONArray("URL").length()==0)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:02:13+00:00Added an answer on May 31, 2026 at 8:02 pm

    JSON objects and arrays are instances of JSONObject and JSONArray, respectively. Add to that the fact that JSONObject has a get method that will return you an object you can check the type of yourself without worrying about ClassCastExceptions, and there ya go.

    if (!json.isNull("URL"))
    {
        // Note, not `getJSONArray` or any of that.
        // This will give us whatever's at "URL", regardless of its type.
        Object item = json.get("URL"); 
    
        // `instanceof` tells us whether the object can be cast to a specific type
        if (item instanceof JSONArray)
        {
            // it's an array
            JSONArray urlArray = (JSONArray) item;
            // do all kinds of JSONArray'ish things with urlArray
        }
        else
        {
            // if you know it's either an array or an object, then it's an object
            JSONObject urlObject = (JSONObject) item;
            // do objecty stuff with urlObject
        }
    }
    else
    {
        // URL is null/undefined
        // oh noes
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an PHP array parsed from a JSON string that looks like this:
I have a JSON string (from PHP's json_encode() that looks like this: [{id: 1,
I’m new to android,I have an activity class which fetches Json string from url
I have a google map which I am getting json data from. I want
I'm getting this json string info from the server : {members:[[sd2840d,Johny],[jkld341,Marry]]} So I store
Hi I have created an activity which pulls data from json format text and
i am getting json data from this link now i want data from html_instructions:
I'm receiving a Json string back from a http request that looks something like
I am getting a response String from server like below { "name": "Json", "detail":
I am getting this JSON string from an ASP.Net webservice: {d:{Table:[{col1:123,col2:name,col3:name,col4:100,col5:\/Date(1153033200000)\/},{col1:123,col2:name,col3:name,col4:101,col5:\/Date(1153033200000)\/},{col1:123,col2:name,col3:name,col4:102,col5:\/Date(1153033200000)\/}]}} In my jQuery

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.