0

I need help with parsing json string in Java Android Appl.

Text of JSON file:

{"data":{"columns":["location_id","name","description","latitude","longitude","error","type","type_id","icon_media_id","item_qty","hidden","force_view"],"rows":[[2,"Editor","",43.076014654537,-89.399642451567,25,"Npc",1,0,1,"0","0"],[3,"Dow Recruiter","",43.07550842555,-89.399381822662,25,"Npc",2,0,1,"0","0"] [4,"Protestor","",43.074933,-89.400438,25,"Npc",3,0,1,"0","0"],[5,"State Legislator","",43.074868061524,-89.402136196317,25,"Npc",4,0,1,"0","0"],[6,"Marchers Bascom","",43.075296413877,-89.403374183615,25,"Node",22,0,1,"0","0"] [7,"Mary","",43.074997865584,-89.404967573966,25,"Npc",7,0,1,"0","0"]]},"returnCode":0,"returnCodeDescription":null}

How can get values: location_id, name, latitude, longitude. Thanks, Michal.

Michal
  • 113
  • 1
  • 4
  • 13
  • 1
    Duplicate of http://stackoverflow.com/questions/2818697/sending-and-parsing-json-in-android – Vlad Jul 21 '11 at 08:58
  • @THelper yes exactly, he should atleast do google first. – Paresh Mayani Jul 21 '11 at 09:09
  • 1
    @THelper et al: With all due respect but I can't see a single line in the question that requests anyone to provide code. I do notice that the question has been modified, which leaves room for misunderstandings from my side, in which case I apologise. Nevertheless I think one should keep the hostile comments to a bare minimum. Please respect that the thread creator is a new member. If one really wants to help, then please do so in a bit more educative and friendly tone... – dbm Jul 21 '11 at 09:59
  • @dbm. You're right. Now that I reread my own comment it looks more "hostile" than I intended. Sorry for that. – THelper Jul 21 '11 at 10:17
  • @THelper - looks like link rot has occurred - the domain androidcompetencycenter.com is for sale. :-/ – Norman H Apr 28 '15 at 18:27
  • @NormanH thanks for reporting! I'll remove my comment (and link) since it is obsolete. – THelper Apr 28 '15 at 21:22

5 Answers5

14

Using manual parsing you can implement it like this:

            JSONArray  pages     =  new JSONArray(jsonString);
            for (int i = 0; i < pages.length(); ++i) {
                JSONObject rec = pages.getJSONObject(i);
                JSONObject jsonPage =rec.getJSONObject("page");
                String address = jsonPage.getString("url");
                String name = jsonPage.getString("name");
                String status =  jsonPage.getString("status");
}

in your case note that your outer elemnt data is type of JSONObject and then you have a JSONArray

mine json file:

[{"page":{"created_at":"2011-07-04T12:01:00Z","id":1,"name":"Unknown Page","ping_at":"2011-07-04T12:06:00Z","status":"up","updated_at":"2011-07-04T12:01:00Z","url":"http://www.iana.org/domains/example/","user_id":2}},{"page":{"created_at":"2011-07-04T12:01:03Z","id":3,"name":"Down Page","ping_at":"2011-07-04T12:06:03Z","status":"up","updated_at":"2011-07-04T12:01:03Z","url":"http://www.iana.org/domains/example/","user_id":2}}] 

note that mine starts from [, which means an array, but yours from { and then you have [ array inside. If you run it with a debugger, you can see exactly what´s inside your json objects.

There are also better approaches like:

  1. Jackson
  2. Jackson-JR (light-weight Jackson)
  3. GSON

All of them can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

Mobile Developer
  • 5,730
  • 1
  • 39
  • 45
  • I'm trying to use Your code and modfied it, but it's still doesn't work. Can you show me json file of yours example? – Michal Jul 21 '11 at 11:44
  • [{"page":{"created_at":"2011-07-04T12:01:00Z","id":1,"name":"Unknown Page","ping_at":"2011-07-04T12:06:00Z","status":"up","updated_at":"2011-07-04T12:01:00Z","url":"http://www.iana.org/domains/example/","user_id":2}},{"page":{"created_at":"2011-07-04T12:01:03Z","id":3,"name":"Down Page","ping_at":"2011-07-04T12:06:03Z","status":"up","updated_at":"2011-07-04T12:01:03Z","url":"http://www.iana.org/domains/example/","user_id":2}}] note that mine starts from [, which means an array, but yours from { and then you have [ array inside – Mobile Developer Jul 21 '11 at 12:40
3

First of all, you need to know about Json parsing in android, so for that first read this: JSONObject, in that class, you will see the below methods:

  1. getJSONArray(String name)
  2. getJSONObject(String name)
  3. getString(String name)

and many more methods to be used while implementing JSON parsing in android.

Update:

And if you are still confused then click on below link to have many examples available on web: Android JSON Parsing

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
0

You need to use the GSON lib http://code.google.com/p/google-gson/

Object Examples

class BagOfPrimitives {
  private int value1 = 1;
  private String value2 = "abc";
  private transient int value3 = 3;
  BagOfPrimitives() {
    // no-args constructor
  }
}

(Serialization)

BagOfPrimitives obj = new BagOfPrimitives();
Gson gson = new Gson();
String json = gson.toJson(obj);  
==> json is {"value1":1,"value2":"abc"}

Note that you can not serialize objects with circular references since that will result in infinite recursion.

(Deserialization)

BagOfPrimitives obj2 = gson.fromJson(json, BagOfPrimitives.class);   
==> obj2 is just like obj
BelmonduS
  • 104
  • 3
0

If you mean to navigate easily the Json Tree, you can use JSON Path, that is query system, similar to XPath to XML, that you can use to pick some elements in a json tree using text expressions.

http://code.google.com/p/json-path/ That's a good implementation

If you just mean that you want to parse that JSon you can use Gson from google (that is compatible with Android I guess).

gotch4
  • 13,093
  • 29
  • 107
  • 170
0

This contains a complete example for your case.

Oh Chin Boon
  • 23,028
  • 51
  • 143
  • 215