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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:30:47+00:00 2026-06-05T05:30:47+00:00

I have a JSONParser that parses to a string pulled from a raw recourse

  • 0

I have a JSONParser that parses to a string pulled from a raw recourse folder. It worked perfictly fine when it was within an activity. Since I need to reuse it in other activities, I moved it into its own Class.

I cant figure why or what I have wrong now. Its virtually unchanged…?
Please take a look at my method to see what you think. Thnx

Fragment Activity:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        InputStream file = getResources().openRawResource(R.raw.regulatory_list);
        JSONParser jParser = new JSONParser();
        JSONArray json = jParser.getJSONFromFile(file);
        callback(jParser);
    }

The Parser Class:

    public class JSONParser {

    static JSONArray jArray = null;

    // constructor
    public JSONParser() {

    }

    public JSONArray getJSONFromFile(InputStream file) {

        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(file, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                file.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        String jsonString = writer.toString();

        // try parse the string to a JSON object
        try {
            jArray = new JSONArray(jsonString);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jArray;
    }

}

Error from JSONException to Log:

06-08 16:04:21.504: E/JSON Parser(27082): Error parsing data org.json.JSONException: Value [{"title":"Advisory Circulators","label":"AC","date":"2008-03-03","_id":"1","gotoURL":null,"description":"Provides guidance such as methods, procedures, and practices for complying with regulations and requirements."},{"title":"Airworthiness Directives","label":"AD","date":"2012-06-08","_id":"2","gotoURL":"javascript:navClickListener('bodyContent', dns + '\/wiki\/index.php\/Airworthiness_Directive #content');","description":"Legally enforceable rules that apply to aircraft, aircraft engines, propellers, and appliances."},{"title":"Code of Federal Regulations","label":"CFR","date":"2012-01-31","_id":"3","gotoURL":"javascript:navClickListener('bodyContent',  dns + '\/wiki\/index.php\/Main_Page #content');","description":"Official Rulemaking documents of the CFR in Title 14 and have been published in the Federal Register"},{"title":"Parts Manufacturer Approvals","label":"PMA","date":"2012-01-31","_id":"4","gotoURL":null,"description":"Parts Manufacturer Approvals"},{"title":"Special Airworthiness Info Bulletins","label":"SAIB","date":"2012-01-31","_id":"5","gotoURL":null,"description":"Bulletins issued by manufacturers to provide modification or inspection instructions."},{"title":"Special Federal Aviation Regulation","label":"SFAR","date":"2012-01-31","_id":"6","gotoURL":null,"description":"Official Rulemaking documents that have changed the language of the CFR in Title 14 CFR for aviation."},{"title":"Supplemental Type Certificates","label":"STC","date":"2012-01-31","_id":"7","gotoURL":null,"description":"Document issued by the Federal Aviation Administration approving a product (aircraft, engine, or propeller) modification"},{"title":"Technical Standard Orders","label":"TSO","date":"2012-01-31","_id":"8","gotoURL":null,"description":"Minimum performance standards issued by the FAA for specified materials, parts, processes, and appliances used on civil aircraft."},{"title":"Type Certificate Data Sheets","label":"TCDS","date":"2012-01-31","_id":"9","gotoURL":null,"description":"Repository of Make and Model information of aircraft, engine or propeller including airspeed, weight, and thrust limitations, etc."}] of type org.json.JSONArray cannot be converted to JSONObject

Log after edit:

06-08 16:44:42.744: E/JSON Parser(27515): Error parsing data org.json.JSONException: Value [{"title":"Advisory Circulators","label":"AC","date":"2008-03-03","_id":"1","gotoURL":null,"description":"Provides guidance such as methods, procedures, and practices for complying with regulations and requirements."},{"title":"Airworthiness Directives","label":"AD","date":"2012-06-08","_id":"2","gotoURL":"javascript:navClickListener('bodyContent', dns + '\/wiki\/index.php\/Airworthiness_Directive #content');","description":"Legally enforceable rules that apply to aircraft, aircraft engines, propellers, and appliances."},{"title":"Code of Federal Regulations","label":"CFR","date":"2012-01-31","_id":"3","gotoURL":"javascript:navClickListener('bodyContent',  dns + '\/wiki\/index.php\/Main_Page #content');","description":"Official Rulemaking documents of the CFR in Title 14 and have been published in the Federal Register"},{"title":"Parts Manufacturer Approvals","label":"PMA","date":"2012-01-31","_id":"4","gotoURL":null,"description":"Parts Manufacturer Approvals"},{"title":"Special Airworthiness Info Bulletins","label":"SAIB","date":"2012-01-31","_id":"5","gotoURL":null,"description":"Bulletins issued by manufacturers to provide modification or inspection instructions."},{"title":"Special Federal Aviation Regulation","label":"SFAR","date":"2012-01-31","_id":"6","gotoURL":null,"description":"Official Rulemaking documents that have changed the language of the CFR in Title 14 CFR for aviation."},{"title":"Supplemental Type Certificates","label":"STC","date":"2012-01-31","_id":"7","gotoURL":null,"description":"Document issued by the Federal Aviation Administration approving a product (aircraft, engine, or propeller) modification"},{"title":"Technical Standard Orders","label":"TSO","date":"2012-01-31","_id":"8","gotoURL":null,"description":"Minimum performance standards issued by the FAA for specified materials, parts, processes, and appliances used on civil aircraft."},{"title":"Type Certificate Data Sheets","label":"TCDS","date":"2012-01-31","_id":"9","gotoURL":null,"description":"Repository of Make and Model information of aircraft, engine or propeller including airspeed, weight, and thrust limitations, etc."}] of type org.json.JSONArray cannot be converted to JSONObject

The original class that worked:

@Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        InputStream is = getResources().openRawResource(R.raw.regulatory_list);
        Writer writer = new StringWriter();
        char[] buffer = new char[1024];
        try {
            Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
            int n;
            while ((n = reader.read(buffer)) != -1) {
                writer.write(buffer, 0, n);
            }
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        String jsonString = writer.toString();
        callback(jsonString);
}
  • 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-06-05T05:30:49+00:00Added an answer on June 5, 2026 at 5:30 am

    From the Log it is showing your JSON is an array, i.e. it looks like this:

     ["foo":bar]
    

    if it was an object it would look like this:

     {"foo":bar}
    

    The first thing you try and do is this:

     new JSONObject(jsonString);
    

    but it’s an array! So you need to create an array:

     new JSONArray(jsonString);
    

    You could then get the objects by looping over the array.

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

Sidebar

Related Questions

I have a web service that returns a raw string of JSON data (sample
I have an int/string(s) that is passed from one fragment to another. For each
I have some python code for Google App Engine that responds with the string
I have written a JSON parser that takes any valid json string and generates
I have a very large JSON string that I need to parse with in-browser
I have a class that needs to be deserialized from JSON using Jackson. The
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =
Have someone tried out DeCAL in Delphi 2009? I'm thinking about upgrading from 2007,
Have a matrix report now that has Position, Hours and Wages for a location
have not tested on windows. but in ubuntu when u disconnect from the network,

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.