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

  • Home
  • SEARCH
  • 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 7500129
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:04:33+00:00 2026-05-29T20:04:33+00:00

im trying to convert a json object to a string by using gson here

  • 0

im trying to convert a json object to a string by using gson here is some of my code

public void returnJson(){

    TextView one = (TextView) findViewById(R.id.textView1);

    try{
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://10.0.2.2/textures_story_list.php");

            HttpResponse response = httpClient.execute(httpPost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

    }catch(Exception e) {
        one.setText("error3");
    }

    try{


        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"), 8);                      
        StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();

    }catch(Exception e) {
        one.setText("error2");          
        }

    try{
        JSONArray jArray = new JSONArray(result);
        for(int i = 0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(0);
            //Log.i("log_tag","story_name: "+json_data.getString("story_name") );
            result += "\n" + jArray.getJSONObject(i); 

        }
        one.setText(result);
    }
    catch(JSONException e) {
        one.setText("error1");
    }
        return;


//end of returnJson()   
}

this is what is outputs into the text view

[{"story_name":"Story One"},{"story_name":"Story Two"},{"story_name":"Story Three"},{"story_name":"Story Four"},{"story_name":"Story Five"},{"story_name":"Story Six"}]

{"story_name":"Story One"}
{"story_name":"Story Two"}
{"story_name":"Story Three"}
{"story_name":"Story Four"}
{"story_name":"Story Five"}
{"story_name":"Story Six"}

exactly like that, i need to know why it outputs it twice and which codes cause each output, then i also need to know how to change this with gson so it outputs

Story One
Story Two
Story Three
Story Four
Story Five
Story Six
  • 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-29T20:04:34+00:00Added an answer on May 29, 2026 at 8:04 pm

    First, this line seems to have no purpose….

    JSONObject json_data = jArray.getJSONObject(0);
    

    The output appears twice because the line

    [{"story_name":"Story One"},{"story_name":"Story Two"},{"story_name":"Story Three"},{"story_name":"Story Four"},{"story_name":"Story Five"},{"story_name":"Story Six"}]
    

    is already in the result String when you start “appending” the others.

    If the output is as simple as this, all you need to do is :

    String storyNames = "";
    for(int i = 0;i<jArray.length();i++){
            storyNames += jArray.getJSONObject(i).getString("story_name"); 
    }
    

    You don’t really need Gson for something so simple.

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

Sidebar

Related Questions

I am trying to convert some xml into a json object using PHP. This
I am trying to convert an object into JSON using the GSON library on
Trying to convert a JSON string into an object in C#. Using a really
I am using the JQuery json plugin and trying to convert a custom object
I'm trying to parse JSON string using Boost Spirit store JSON object into recursive
I'm using Gson to convert Json to an object in Java. Json that i'm
Hello i'm trying to convert my Java Object to a Json string to use
I'm trying to convert JSON to C# object using Json.NET. The object looks like
I'm trying convert jQuery Data object to JSON string. I tried: $.data(plant).toJSON() JSON.stringify($.data(plant)) but
I'm trying to convert some code that worked great in VB, but I can't

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.