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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:29:15+00:00 2026-06-09T05:29:15+00:00

First code: Bond[] bonds = null; try { JSONArray jsonArray = new JSONArray(result); bonds

  • 0

First code:

    Bond[] bonds = null;
    try
    {
        JSONArray jsonArray = new JSONArray(result);
        bonds = new Bond[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++)
        {
            JSONObject json = jsonArray.getJSONObject(i);
            bonds[i] = new Bond(json);
        }
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }

Second:

    Announcement[] announcements = null;
    try
    {
        JSONArray jsonArray = new JSONArray(result);
        announcements = new Announcement[jsonArray.length()];
        for (int i = 0; i < jsonArray.length(); i++)
        {
            JSONObject json = jsonArray.getJSONObject(i);
            announcements[i] = new Announcement(json);
        }
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }

I am thinking about extracting a method which will cover these two codes. I think the method should look more or less like this:

static Object[] getObjectsArray(String jsonString, Class<?> cls)
{
    Object[] objects = null;
    try
    {
        JSONArray jsonArray = new JSONArray(jsonString);
        objects = (Object[]) Array.newInstance(cls, jsonArray.length());
        for (int i = 0; i < jsonArray.length(); i++)
        {
            JSONObject json = jsonArray.getJSONObject(i);
            objects[i] = new Announcement(json); // FIXME: How to pass "json" arg to the constructor with cls.newInstance()?
        }
    }
    catch (JSONException e)
    {
        e.printStackTrace();
    }
    return objects;
}

So later instead of first code I can just call Bond[] bonds = (Bond[]) getObjectsArray(jsonArray, Bond).

This is the most problematic line:

objects[i] = new Announcement(json); // FIXME: How to pass "json" arg to the constructor with cls.newInstance()?
  • 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-09T05:29:16+00:00Added an answer on June 9, 2026 at 5:29 am

    You can use the following syntax to use a constructor with arguments (I assume the argument of the constructor is a JSONObject and that the constructor is public – if it is not, use the getDeclaredConstructor method):

    Class<Announcement> cls = Announcement.class; //the second argument of your method
    objects[i] = cls.getConstructor(JSONObject.class).newInstance(json);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First my code $.getJSON(./posts/vote/ + postId + /1, null, function(result) { if (result.result ==
First code : public static int pitagoras(int a, int b) { return (int) Math.sqrt(a*a
Code first: AssetManager mgr = DeviceListActivity.this.getApplicationContext().getAssets(); try { Log.e(Glenn:, address); FileOutputStream fout = mgr.openFd(device/device_address.txt).createOutputStream();
I have this code: ImageButton call = new ImageButton(context); call.setId(9001+result.index); call.setBackgroundResource(R.drawable.small_call); LinearLayout.LayoutParams call_params =
I am very new To EntityFramework and having trouble to understand why first code
EDIT: SOLVED! answer below first code box When i try to connect with a
I'm totally new in WPF. It's kind of confusing, why the first code snippet
I run following working code: Session session = null; try { SessionFactory sessionFactory =
When I run the first code and press ctrl-c immediately there will be no
Why does the first code print DCBA and the second ABCD? Is 0x44434241 not

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.