Accessing an object from JSON in Android

I have a question that I am a little bit confused about. I am quite new to JSON and getting JSON values in the android API. I am trying to access an array within the response I get. the JSON code I am getting is something like this:

Response: { "event": { "participants": [] }, "status": "success" }

How would I access the participants array and store their values. This is what I am trying at the moment... but I dont appear to be getting what I want.

try{ //get the JSON values from the URL. JSONObject json = jParser.getJSONFromUrl(""+"somevar="+someJavaStringVar); json_event = json.getJSONObject("event"); JSONArray json_array_participants = json_event.getJSONArray("participants"); } catch(JSONException e) { }

The thing I am mostly confused about is... what is the arrays type equivalent to. Any advice or reasoning as to the correct way to get ahold of that variables value would be great... thanks guys.. :).

-------------Problems Reply------------

Think JSON is really just a key-value pairing. The JSONArray type is just an array full of objects (like Object[]) - it has no idea what the objects it contains are or what they're to be used for. Its up to you to assign meaning to the JSON stream based on what you know of the source. From what I see of your code, most of it looks fine, though I don't know what your jParser.getJSONFromURL() is doing. Typically, you would build the JSON from the response string like so:

String jsonString = getJSONFromUrl(""+"somevar="+someJavaStringVar);

JSONObject json = new JSONObject(jsonString)

JSONObject json_event = json.getJSONObject("event");

JSONArray json_array_participants = json_event.getJSONArray("participants");

You can iterate through the array like any other array to get subobjects or whatever:

for(int i=0; i < json_array_participants.getLength(); i++) {
JSONObject participant = json_array_participants.getJSONObject(i);
// Do stuff

As a side note - I WOULDN'T use GSON until you understand the underlying protocol, at least a little - because you never know when you might want to parse your JSON from a different language for some reason.

I would strongly recommend to use gson instead as your preferred parser since it will do all the job of serializing and deserializing for you except creating the domain objects. This tutorial should get you going:

This will depend on what the server is supposed to return. It could be an array of anything and if this is a public service, there should be a specification to go off of.

If you are in charge of the server portion as well, and you have a backing object, Google's GSON library is extremely easy to use. It will also keep type information straight.

Category:android Views:0 Time:2012-04-13

Related post

  • Convert object to JSON in Android 2011-04-06

    Is there a simple method to convert any object to JSON in Android? --------------Solutions------------- Most people are using gson: Gson gson = new Gson(); String json = gson.toJson(myObj); public class Producto

  • Accessing nested objects in JSON feed - Sencha Touch 2011-04-27

    I'll begin with the usual disclaimer: new to Sencha Touch/working with JSON, floundering in the dark. Any help or prodding in the right direction is appreciated more than you know! I'm trying to get my app to fetch data from a public Google Spreadshe

  • Accessing Nested Objects in Json 2009-05-14

    I have a custom object which contains other items (ie arrays, strings, other types of objects). I am not sure how to traverse the object to iterate and list all of the object types, keys, and values of the nested items. Second to this issue I don't k

  • android how to convert the json object to json array 2011-08-25

    i'm trying to create an application about convert the json object to json array. If it's possible means tell me the way, otherwise, tell the alternate way. Thanks in advance. --------------Solutions------------- try this: JSONArray jArrayObject = new

  • How to access child element of json object using node.js 2014-12-08

    In my node.js app I retrieve value from mongodb server and want to convert in CSV file.The parent element is easily accessed frrom database and dispalyed in CSV file but the sub element is not displayed and can't be accessed.. JSON structure: "name"

  • How to parse json response android 2012-04-10

    How to parse this below json response. I am developing a booking app. The response i got from the server is {"HotelInformationResponse": { "@hotelId": "210985", "customerSessionId": "0ABAA826-9AAF-5791-3692-A03326791310", "HotelSummary": { "@order":

  • How to parse nested JSON objects with JSON framework and Objective-C/iPhone/Xcode? 2009-03-28

    I'm writing an iPhone native app using the JSON framework. My app is accessing web services using JSON. The JSON data we send has nested objects, below is an example of the data served up: { "model": { "JSONRESPONSE": { "authenticationFlag": true, "s

  • Serializing business objects as JSON 2009-04-03

    I'm trying to serialize my business objects into JSON for consumption by a Javascript application. The problem is is that I'm trying to keep my business objects "pure" in the sense they are not aware of data access or persistence. It seems to me that

  • Access Model object in jQuery 2009-11-30

    I have a Model, Customers, in which I have a list property, PayHistory. I need to bind the table with the PayHistory object using jQuery in my ASP.NET MVC (C#) application. I am binding the table using jQuery as: <table width="100%" border="0" cel

  • Passing a JavaScript object using addJavascriptInterface() on Android 2010-02-12

    Is it possible to pass a JavaScript object from JavaScript to Java using addJavascriptInterface()? Something along these lines: var javaScriptObject = {"field1":"string1", "field2":"string2"}; JavaScriptInterface.passObject(javaScriptObject); How wou

  • Sending and Parsing JSON in Android 2010-05-12

    In the application I am developing, I would like to send messages in the form of JSON objects to a Django Server and parse the JSON response from the server and populate a custom listview. From the little JSON knowledge I have, I thought this format

  • How to parse an object to JSON using jQuery? 2010-08-24

    I know to use eval() to parse JSON to an object, but how do I parse an object to JSON format using JavaScript? Thanks. --------------Solutions------------- The newer browsers support JSON.stringify. You can also download and include it yourself. var

  • How to access JavaScript objects in code behind (C#/ASP.NET) 2010-10-15

    I am using the web kit browsers local database to temporarily store some data and when I want to access it I create an object function patientSelectHandler(transaction, results) { var row = results.rows.item(0); var patient = new Object();

  • iterate over list of complex objects in JSON? 2010-12-09

    I build a list of objects to put into a JSON result: Person personA = new Person("nameA", "age"); Person personB = new Person("nameB", "age"); Person personC = new Person("nameC", "age"); List<Car> cars = new ArrayList<Car>(); cars.add(ne

  • Forward slashes in stdClass object parsing JSON? 2011-04-21

    I'm trying to access data within a JSON object I create using json_decode (based on a result from a URL). The response is: stdClass Object ( [/guid/9202a8c04000641f8000000016f4c9cd] => stdClass Object ( [code] => /api/status/ok [result] => s

  • How to access an object using a variable string? 2011-05-08

    I have an object whose name is mural. I have assigned the name mural to the title of an anchor element. When user clicks and element I want to store the title of the anchor in a variable called sprite. I then want to access that object whose name cor

  • Deserializing a mixed list of objects from JSON 2011-05-24

    I'm using the DataContractJsonSerializer to deserialize objects from an external service. In most cases, this has worked great for me. However, there is one case where I need to deserialize JSON that contains a list of objects that all inherit from t

  • access some elements of JSON string 2011-06-16

    This is what I did in code behind: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public static string CheckInsertRecord(String EventType, String BeginDate, String EndDate) { NCDCPoint ncdc = new NCDCPoint(); CEOSurveyDataContext CD

  • Parsing JSON in Android 2011-06-22

    I am about to work with JSON for the first time. Previously I worked on parsing XML in Android. How is it different with JSON? Suggest me some good tutorials for the same. Stone --------------Solutions------------- You actually mean JSON right? If yo

Copyright (C), All Rights Reserved.

processed in 0.145 (s). 11 q(s)