0

I have a problem with my code,

I have a json array

                [{"Response":{"data":"sibin1"}},{"Response":{"data":"sibin2"}},
                              {"Response": {"data":"sibin3"}}]

And iam trying to extract the json data using the below code,Here i added only some parts of the coode

                 JSONArray  finalResult = new JSONArray(tokener);
                 int finalresultlengt=finalResult.length();
                 JSONObject json_data = new JSONObject();
      for (int i = 0; i < finalResult.length(); i++) 
            {

          json_data = finalResult.getJSONObject(i);
          System.out.println("json dataa"+json_data.names().toString());
          JSONObject menuObject = json_data.getJSONObject("Response");
          result=   menuObject.getString("data");   
             System.out.println(result);
              }

The code is worked very well

when the value of

                    i=0 ,result is sibin1
                    i=1 ,result is sibin2
                    i=2 ,result is sibin3

But my problem is , i need to store the result in a string array of length finalresultlength inside the given for loop, also i need to print the values in the string array in a for loop outside the given for loop if anybody knows please help me...........

4
  • 1
    Please refactor your question in order to make it readable. Fix indentation and try to put some punctuation. Commented Feb 14, 2013 at 9:18
  • please make a question clearly Commented Feb 14, 2013 at 9:19
  • Before your for loop declare a string array String[] dataStrings = new String[finalResultLength]; then as the last line of your for loop add dataStrings[i]=result; Commented Feb 14, 2013 at 9:20
  • i already tried this exact code, but it shows null value Commented Feb 14, 2013 at 9:22

1 Answer 1

3

You could do this way as well.

Create an ArrayList of size 'finalresultlengt' and the add the values in.

 list.add(result);  // variable 'result' in your case is the value from JSON

If you have more values to be added, create a POJO class.

class POJO {
    private String dataVal;

    public void setDataVal(String dataVal) {
        this.dataVal = dataVal;
    }

    public String getDataVal() {
        return dataVal;
     }
 }

Then create an ArrayList of type POJO.

 ArrayList<POJO> list = new ArrayList<POJO>(finalresultlengt);

EDIT

JSONArray  finalResult = new JSONArray(tokener);
int finalresultlengt=finalResult.length();
JSONObject json_data = new JSONObject();
ArrayList<String> list = new ArrayList<String>(finalresultlengt);

for (int i = 0; i < finalResult.length(); i++) {
      json_data = finalResult.getJSONObject(i);
      System.out.println("json dataa"+json_data.names().toString());
      JSONObject menuObject = json_data.getJSONObject("Response");
      result=   menuObject.getString("data");
      list.add(result);   
 }

Populate values from ArrayList.

 for(String value : list) 
      System.out.println(value);
Sign up to request clarification or add additional context in comments.

8 Comments

can you please show an example for arraylist, i want to add only few data
Hi Renjith i use for(String value : list) System.out.println(value); for printing, but it print only the 3 rd element and i print the size of list it shows 1 why?????????
check the value of finalresultlengt
can you please give me a solution to my problemmmm
try printing the list size each time you add! Check for any unusual behaviour
|

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.