Philip
Philip

Reputation: 1078

How to Display JSONObject in Android?

I just requested a JSONObject from an URL and I wonder two things. Is a JSONObject like a JSONArray, or not? Which should I request? And is there a way to display this JSONObject somehow like var_dump in PHP? Just to check whether it worked?

Help is much appreciated! Philip

Upvotes: 3

Views: 12658

Answers (2)

Luca Sepe
Luca Sepe

Reputation: 2445

  1. JSONObject is different from a JSONArray
  2. Just to ckeck you can invoke .toString() on your JSONObject (or JSONArray) instance.

    JSONObject jo = ...

    Log.d("TAG", jo.toString());

Upvotes: 6

DeeV
DeeV

Reputation: 36045

JSONArray is an array of JSONObjects. A JSONObject can hold one or more JSONArrays. Generally you pull a single JSONObject root node and break it up from there.

You can print the JSON by using [JSONObject#toString()][1]. You can also use JSONObject#toString(int indentSpaces) to put it in a more readable form like so:

 {
     "query": "Pizza",
     "locations": [
         94043,
         90210
     ]
 }

Upvotes: 3

Related Questions