0

I don't know much about JSON parseing. The JSON API is like that.

[
   "Text A" , "Text B" , "Text C"
]

I want to get all the texts from api directly from server.

How can I get these texts (Text A ,Text B , Text C) as an array?

5
  • 1
    Post your proper json response Commented May 11, 2015 at 12:55
  • I'm using this api proverbs-app.antjan.us How can I parse them into an array Commented May 11, 2015 at 12:57
  • @AnjaliTripathi ..what do you mean by proper? Commented May 11, 2015 at 12:57
  • its a valid json...and basic structure is shown by original author Commented May 11, 2015 at 12:58
  • @LinLin you can also used List and add data to there like as List<String> list = new ArrayList<String>(); for (int i=0; i<jsonArray.length(); i++) { list.add( jsonArray.getString(i) ); } Commented May 11, 2015 at 13:08

2 Answers 2

2

Its a simple basic jsonarray...you need to take jsonarray object and store the whole jsonarray then you need to iterate jsonarray indexes and save the string to string array

JSONArray mainjson=new JSONArray(<jsonstring>);
String text[]=new String[mainjson.length()];
for(int i=0;i<mainjson.length();i++)
{
     text[i]=mainjson.getString(i);
}
Sign up to request clarification or add additional context in comments.

6 Comments

Sir , can you explain me more detail? I'm using this api proverbs-app.antjan.us
don;t understant what you said... acc to your json...all the text is saved inside stringarray "text" ..then you can use this array acc to your need
SIr , I want to fetch all the data from here. proverbs-app.antjan.us And store all of them in an array
@AngadTiwari is correct LinLin you just have to insert your String of Array and run that code, i think it run correctly... JSONArray mainjson=new JSONArray(<YOUR ARRAY STRING IS HERE>); . .
I want to fetch this string of array directly from server.
|
0

Using AQuery Library You can Do it Simply Like this, First Download Aquery.jar File & Paste it in your Lib Folder of Project.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AQuery aq = new AQuery(this);
    String url = "YOUR URL HERE";

    aq.ajax(url, JSONArray.class, this,
            "jsoncallback");

}

public void jsoncallback(String url, JSONArray jsonArray, AjaxStatus status) {

    String[] values; // this array will store your Strings
    if (jsonArray != null)
    {

            try {

                    for (int i = 0; i < jsonArray.length(); i++) 
                    {
                        value[i] = jsonArray.getString(i);
                    }

                } 
            catch (JSONException e) 
                { // TODO Auto-generated catch block
                    e.printStackTrace();
                }

    }
    else 
    {
        // Json is Null
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.