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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:22:28+00:00 2026-05-28T01:22:28+00:00

I created a a JSON Array from a normal array of user defined object.How

  • 0

I created a a JSON Array from a normal array of user defined object.How do i convert the JSONArray back to a normal array of the user-defined type..?
I’m using Json for shared preference in android.Using this code i found on the net:

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

import android.content.Context;
import android.content.SharedPreferences;

public class JSONSharedPreferences {
    private static final String PREFIX = "json";

    public static void saveJSONObject(Context c, String prefName, String key, JSONObject object) {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(JSONSharedPreferences.PREFIX+key, object.toString());
        editor.commit();
    }

    public static void saveJSONArray(Context c, String prefName, String key, JSONArray array) {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putString(JSONSharedPreferences.PREFIX+key, array.toString());
        editor.commit();
    }

    public static JSONObject loadJSONObject(Context c, String prefName, String key) throws JSONException {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        return new JSONObject(settings.getString(JSONSharedPreferences.PREFIX+key, "{}"));
    }

    public static JSONArray loadJSONArray(Context c, String prefName, String key) throws JSONException {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        return new JSONArray(settings.getString(JSONSharedPreferences.PREFIX+key, "[]"));
    }

    public static void remove(Context c, String prefName, String key) {
        SharedPreferences settings = c.getSharedPreferences(prefName, 0);
        if (settings.contains(JSONSharedPreferences.PREFIX+key)) {
            SharedPreferences.Editor editor = settings.edit();
            editor.remove(JSONSharedPreferences.PREFIX+key);
            editor.commit();
        }
    }
}

I’m trying to convert a user defined object array into jsonarray and storing it in jsonshared preference and later trying to retrive it.Having problem knowing how to retrive it.
Thanks.

  • 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-28T01:22:28+00:00Added an answer on May 28, 2026 at 1:22 am

    If you’re using JSONObject that comes with Android its tedious to convert from User defined types to JSONObject/JSONArray then back again. There are other libraries out there that will do this transformation automatically so it’s simple one or two line to decode/encode JSON.

    ProductLineItem lineItem = ...;
    JSONObject json = new JSONObject();
    json.put( "name", lineItem.getName() );
    json.put( "quantity", lineItem.getCount() );
    json.put( "price", lineItem.getPrice() );
    ... // do this for each property in your user defined class
    String jsonStr = json.toString();
    

    This could all be encapsulated within ProductLineItem.toJSON(). Parsing is similar. I like to create a constructor that takes a JSONObject and creates the object like: ProductLineItem obj = new ProductLineItem( jsonObject ):

    public class ProductLineItem {
        private String name;
        private int quantity;
        private float price;
    
       public MyObject( JSONObject json ) {
          name = json.getString("name");
          count = json.getInt("quantity");
          price = json.optFloat("price");
       }
    }
    

    Handling arrays is very much the same. So something like:

    public class ShoppingCart {
    
         float totalPrice;
         List<Rebate> rebates = new ArrayList<Rebate>();
         List<ProductLineItem> lineItems = new ArrayList<ProductLineItem>();
    
    
        public ShoppingCart( JSONObject json ) {
            totalPrice = json.getFloat("totalPrice");
    
            for( JSONObject rebateJson : json.getArray("rebates") ) {
                rebates.add( new Rebate( rebateJson ) );
            }
    
            for( JSONObject productJson : json.getArray("lineItems") ) {
                lineItems.add( new ProductLineItem( productJson ) );
            }
        }
    
        public JSONObject toJSON() {
            JSONObject json = new JSONObject();
            json.put("totalPrice", totalPrice );
    
            JSONArray rebatesArray = new JSONArray();
            for( Rebate rebate : rebates ) {
                rebatesArray.put( rebate.toJSON() );
            }
    
            JSONArray lineItemsArray = new JSONArray();
            for( ProductLineItem lineItem : lineItems ) {
                lineItemsArray.put( lineItem.toJSON() );
            }
    
            json.put( "rebates", rebatesArray );
            json.put( "lineItems", lineItemsArray );
    
            return json;
        }
    }
    

    You can see just for a simple 2 objects this boiler plate code is quite significant. So you can continue to do this or you can use a library that handles all of this for you:

    http://flexjson.sourceforge.net

    // serialize
    String json = new JSONSerializer().serialize( shoppingCart );
    // deserialize
    ShoppingCart cart = new JSONDeserializer<ShoppingCart>().deserialize( json, ShoppingCart.class );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I want to create a JSON array. I have tried using: JSONArray jArray
I have created a json object from ruby with cobravsmongoose, however the attributes have
I have an array that gets created from JSON. The array looks like this:
I'm parsing a JSON array returned from YouTube, and while doing so I created
I have a JSON array like this: { forum:[ { id:1, created:2010-03-19 , updated:2010-03-19
I have a stdClass object created from json_decode that won't return the right number
I have a news feed where items in the feed are created from JSON
I am parsing json object from facebook. In my facebook json object, there are
I have a low level caching mechanism which receives a json array from a
code //array store the markers var googleMarker = []; //this function get json object

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.