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 6970691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:44:33+00:00 2026-05-27T16:44:33+00:00

I have a strange problem in my Android code when I try to pass

  • 0

I have a strange problem in my Android code when I try to pass json-smart JSONObject or JSONArray as a serializable data as Intent serializable extra. Json-smart is very fast and lean JSON parser implementation, I highly recommend it. JSONObject extends HashMap<String, Object> and JSONArray extends ArrayList<Object> with very little overhead. Nether of these objects override Object#write or Object#read

Here’s the problem:

If I use these objects in say Fragment#onSaveInstanceState(String, JSONObject) everything works just fine. If I serialize/deserialize these objects in plain Java sample project, again it works as intended. However if I use Intent#putExtra(String, JSONObject) and then try to get my JSONObject by doing

JSONObject json = (JSONObject) intent.getSerializableExtra("JSON");

I will get ClassCastException because what is returned by the method is a plain HashMap (and in case of JSONArray it’s ArrayList). Moreover if I look inside the map/array the content is completely stripped of any references to JSONObject/JSONArray and replaced with HashMaps/ArrayLists

I filed the ticket and provided example project but unfortunately author closed it without resolution, so I’m simply trying to get to the bottom of it. If you go to the ticket it has simple project attached.

Is there a way out of this? For now I have to convert JSONObject or JSONArray to String and reparse it back to the object such as in:

JSONArray feeds = (JSONArray) JSONValue.parse(intent.getStringExtra(RAW_FEED));
  • 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-27T16:44:33+00:00Added an answer on May 27, 2026 at 4:44 pm

    Implement android.os.Parcelable:

    http://developer.android.com/reference/android/os/Parcelable.html
    

    Basically, you put a little extra code into your object to explain how to serialize and deserialize it. That’s why it’s losing your class information, it doesn’t know how to serialize it correctly. If I were you I would pass a value object through your intent verses a JSON object simply because know you know exactly what type of object you are passing. You retrieve your Parcelable this way:

    getIntent().getExtras().getParcelable(key); 
    

    I know it probably seems like a little extra code but I actually think this is a pretty clean way to share data between activities. You are passing a strong type, you specify how to deflate/inflate it, and your activities don’t really have to have any working knowledge of each other.

    In your value object you just fill in the blanks like this:

    /** Parcelable implementation: deserialize object
     * @param in Parcel object containing the data for this object
     * @return a Person object  */
    public Person( Parcel in ) {
        String[] data = new String[2];
        in.readStringArray(data);
        this.firstName = data[0];
        this.lastName = data[1];
    }
    
    /** Parcelable implementation code */
    public int describeContents() {
        return 0;
    }
    
    /** Parcelable implementation: Serialize object to be passed between activities via Intent
     * @param dest The parcel to transport the object
     * @param flags refer to Parcelable api
     * @return nothing */
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeStringArray( new String[] {
                this.firstName,
                this.lastName,      
        });
    }
    

    Hope this helps, might be little more code than what you were looking for but it’s a real clean solution because when the target activity gets the object, you don’t have to worry about deserializing which means cleaner code in your activity. Fat model, clean views 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strange problem with some documents on my webpage. My data is
I have a strange problem that I could not solve. When I try to
I have a very strange problem with Android animations, I tried many different approaches
I have a strange problem with my Android app. When I start it from
I'm making an Android app and have a strange problem. I need a ViewFlipper
I have a strange problem with developing in Scala on Android. I'm using sbt
I have a strange problem with an HTC Desire, Android 2.2 and 2.3, Vodafone
I have a strange problem in my android app. I have an activity which
I have a strange problem when accessing android sqlite datbase. I have a line
I have an extremely strange Problem, while trying to develop for android on eclipse

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.