0

I got the following JSON data from my API:

enter image description here

Json array and alot of json objects in it.

How do I get a json array without a name?

This is what I tried:

//Parsing the fetched Json String to JSON Object
 j = new JSONObject(response);
result = j.getJSONArray('');
 for(int i=0;i<j.length();i++){
            try {
                //Getting json object
                JSONObject json = j.getJSONObject(i);
                Log.d("tag", "NAME IS: " +json.getString("name"));
}
}

the json variable stores all the json data!

5
  • Try to replace the ' ' with " ", and if it doesn't work try with "" Commented Jul 30, 2016 at 17:54
  • Doesn't work. If I'm changing my API and name my array, and type in there "myArray", it works.. without a name - it doesnt :S Commented Jul 30, 2016 at 17:59
  • Does your response begin with this array ? Commented Jul 30, 2016 at 18:19
  • I change my API to output "myArray [... because an empty one doesn't work with Android, or a way I don't know. Commented Jul 30, 2016 at 18:23
  • You didn't answer my question, is you no-name array the root of your Json response ? If so, just follow USKMobility answer and it will work Commented Jul 30, 2016 at 18:24

2 Answers 2

3

JSONArray has a constructor which takes a String source.

JSONArray array = new JSONArray(yourJSONArrayAsString);

Then you can get each object using for loop.

JSONArray array;
    try {
        array = new JSONArray(yourJSONArrayAsString);

        for(int i=0;i<array.length();i++){
            JSONObject obj = array.getJSONObject(i);
            // get your data from jsonobject
        }
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

But my response is a json object I guess.. I do j = new JSONObject(response);
-1

create JSONParser class which contains json parsing code.

Mention in Your MainActivity.java

JSONParser jsonparser=new JSONParser(); JSONObject jsonObject=jsonparser.getJSONFromURL(URL);

Now Create separate JSONParser class

class JSONParser
{
   public static JSONObject jsonObject=null;
   public static String json=null;
   InputStream is=null;
   public JSONObject getJSONFromURL(String url)
{ 
    try
{
      HttpClient client=new DefaultHttpClient();
      HttpPost post=new HttpPost(url);
      HttpResponse response=client.execute(post);
      HttpEntity entity=response.getEntity();
      is=entity.getContent();
}
catch(Exception e)
{
  e.printStackTrace();
}
try
{
  BufferedReader br=new BufferedReader(new InputStreamReader(is,"UTF-8"));
  StringBuilder sb=-new StringBuilder();
  String line=null;
  while((br.readLine())!=null)
  {
    sb.append(line+"\n");
  }
 json=sb.toString();
 is.close();
}
catch(Exception e)
{
   e.printStackTrace();
}
 try
{
  jsonObject=new JSONObject(json.subString(json.indexOf("{"),json.lastinddexOf("}")+1));
}
catch(Exception e)
{
   e.printStackTrace();
}
return jsonObject;
}

Now in MainActivity.java

try
{
JSONParser jsonparser=new JSONParser();
JSONObject jsonObject=jsonparser.getJSONFromURL(URL);// URL is a String which contains url
Log.d("Response:",jsonObject.toString());
JSONArray jsonarray=new JSONArray(jsonObject.getString("YourFirstJSONArrayName"));//YourJSONArray contains the response array
for(int i=0;i<jsonarray.length();i++)
 {
   JSONObject c=jsonarray.getJSONObject(i);
   // now get data from c object
 }
// Now getting data from Second Array
 JSONArray jsona=new JSONArray(jsonObject.getString("YourSecondJSONArrayName"));
   for(int j=0;j<jsona.length();j++)
{
    JSONObject c=jsona.getJSONObject(j);
      // now get data from json data from second array
}
 }
catch(Exception e)
{
  e.printStackTrace();
}

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.