Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Exception while reading JSON object

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Exception while reading JSON object

    Error while reading JSON object. Below is my JSON object :
    {"website":"http://developers.facebook.com","link":"http://www.facebook.com/FacebookDevelopers","about":"Grow your app with Facebook\nhttps://developers.facebook.com/ ","id":"19292868552","username":"FacebookDeveloper s","category":"Product/service","cover":{"source":"http://sphotos-b.xx.fbcdn.net/hphotos-ash4/s720x720/377655_10151298218353553_500025775_n.png","cover_i d":"10151298218353553","offset_y":0},"name":"Faceb ook Developers","likes":1141231,"were_here_count":0,"c ompany_overview":"Facebook Platform enables anyone to build social apps on Facebook, mobile, and the web.\n\n","is_published":true,"talking_about_count ":31759}

    Follwong is the error iam getting while reading the JSON object:
    "org.json.JSONException: JSONArray[6] not a string."
    How could i solv the exception?

    Thanks in advance.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Exception while reading JSON object

    You haven't provided any code with which to let us inspect, which leaves things open to guessing. My guess: the sixth element in the JSON array is not a String - it is an array/Map

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception while reading JSON object

    Thanks for the support.

    Below is the code:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.Reader;
    import java.net.URL;
    import java.nio.charset.Charset;
    import java.util.StringTokenizer;
    import java.util.logging.Logger;

    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;

    public class JsonReader {

    private static String readAll(Reader rd) throws IOException {
    StringBuilder sb = new StringBuilder();
    int cp;
    while ((cp = rd.read()) != -1) {
    sb.append((char) cp);
    }
    return sb.toString();
    }

    public static JSONObject readJsonFromUrl(String url) throws IOException, JSONException {
    InputStream is = new URL(url).openStream();
    try {
    BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
    String jsonText = readAll(rd);
    JSONObject json = new JSONObject(jsonText);
    String inputLine = null;
    while ((inputLine = rd.readLine()) != null)
    System.out.println(inputLine);

    return json;
    } finally {
    is.close();
    }
    }

    public static void parseProfilesJson(String the_json){

    try {
    JSONObject myjson = new JSONObject(the_json);

    JSONArray nameArray = myjson.names();
    JSONArray valArray = myjson.toJSONArray(nameArray);
    for(int i=0;i<valArray.length();i++)
    {
    String p = nameArray.getString(i) + "," + valArray.getString(i);
    System.out.println("----p-----"+p);
    //Logger.i("p",p);
    }

    } catch (JSONException e) {
    e.printStackTrace();
    }
    }
    public static void main(String[] args) throws IOException, JSONException {
    JSONObject json = readJsonFromUrl("https://graph.facebook.com/19292868552");

    System.out.println("--json.toString()--- " +json.toString());
    parseProfilesJson(json.toString());
    }
    }

    Thanks.

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Exception while reading JSON object

    As I mentioned above, the sixth element in the JSON object is not a String, it is an array - try using the getJSONArray method

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Exception while reading JSON object

    Ok,Thank you.

Similar Threads

  1. Replies: 0
    Last Post: January 30th, 2013, 04:41 AM
  2. consume the rest service by passing the json object
    By StarRocks in forum Object Oriented Programming
    Replies: 0
    Last Post: January 29th, 2013, 06:28 AM
  3. Exception while reading xlsx file
    By tcstcs in forum Exceptions
    Replies: 1
    Last Post: May 5th, 2011, 05:06 AM
  4. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 06:11 AM
  5. Reading data from a text file into an object
    By surfbumb in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 6th, 2011, 08:37 PM

Tags for this Thread