How to retrieve key/value array from Bundle on Android - android

I'm trying to retrieve data from "extra" key:
Bundle[
{from=1058706545539,
extra={
"ty":"msg",
"d":"sec":1425242647,"usec":763000},
"iL":"86777e87a574c3f068f6525e",
"tU":"7e0a9dbbd1d6ee1795d64fdf",
"iP":"4f26e5f78d042e2224688ed7",
"iM":"dd83db95e764b103b4fec99e"},
message=Oi ,
android.support.content.wakelockid=1,
collapse_key=do_not_collapse
}]
If it was JSon I'd use JSONObject, I don't know how to retrieve the the whole "extra" on a HashMap structure. So that I can use something like that:
String ty = extra.getString("ty");
I receive this bundle from Push Notification.

You can get the string, then make it a JSON Object:
String json = extra.getStringExtra("extra");
Then:
JSONObject jobject = new JSONObject(json);

Related

how to send this JSON Array from android to server using retrofit

I want to send this parameter in request and tried to send a simple array list but that was not working
"Language": [
"string","string","string"
]
If "Language" is key and ["string1","string2","string3"] is value then create method in interface as below.
#FormUrlEncoded
#POST(LINK_API)
Call<ResponseModel> getResponse(#Field("Language") String languageArray);
and call it using interface instance like this :
JSONArray languageArray = new JSONArray();
languageArray.add("string1");
languageArray.add("string2");
languageArray.add("string3");
String langArray = languageArray.toString();
Call<ResponseModel> responseModel = apiObject.getResponse(langArray);
responseModel.enqueue(...);
This will work perfectly.
try post query with field parameter as string..
convert your jsonObject to String using,
String b = json_object.toString();
At Server Side, convert string back to JsonObject or whatever else you need.

How to get all the parameters in following response in Android

I want to get parameters from this response !!
Bundle[{custom={"custom data":{"notification_type":"offer","offer_id":4348}}, from=1013970362419, badge=1, message=birds view, android.support.content.wakelockid=4, collapse_key=do_not_collapse}]
I want offer_id and notification_id , in above notification data
As the response is received in Bundle, you cannot parse it directly. Instead you can get each parameter of bundle using bundle.getString(key) or bundle.getInt(key) whatever type of data is.
So in your response to get offer_id, first of all extract custom parameter as a string and convert it into JSONObject. Then you will be able to get offer_id.
E.g.
JSONObject keyData = new JSONObject(data.getString("custom"));
Gson gson = new Gson();
KeyValueModel keyValue= gson.fromJson(keyData.toString(), KeyValueModel .class);
String offerId = keyValue.getString("offer_id")

How to extract objects from json

I have a scenario where I need only 1 objects out of the entire json.
{"id":"1","first_name":"Steve","last_name":"Holt","user_type":"Teacher","user_key_area":"Math"}
In above I want to extract user_type.
How will I do?
You can use Google GSON to parse the json response and map it to the model directly . Here is the tutorial for the same TUTORIAL
Use this to extract the user_type from the json, pass the your json response in place of response variable.
JsonObject object =new JsonObject(response);
String user_type = object.getString("user_type");
You are getting this json in response you can get 1 object like this:
String value = response.getString("Key Name");
Get Response and that create JsonObject
JsonObject jsonObject =new JsonObject(res);
Create String object and pass string parameter "user_type"
String userType = jsonObject.getString("user_type");

Android - how to format objects in JSON

I have this class:
class Type{
public int id;
public String name;
}
and I want to format object in JSON this way
{[1,"superMarket"],[2,"restaurant "]}
and also put data from JSON to "Type" object
can some one help me please......
You can get an easy to understand example for encoding and parsing json:
Android JSON Tutorial: Create and Parse JSON data
and
JSON in Android - Tutorial
you nee to use a jasonArray and thee jsonObject ro do this..
you can viw this answer
Android create a JSON array of JSON Objects
Type to JSON
JSONObject jsonObj = new JSONObject();
jsonObj.putString("id", id);
jsonObj.putString("name", name);
You can add as many jsonObjs to JSONArray.
JSON to Type
Type type = new Type();
type.setId(jsonObj.optString("id"));
type.setName(jsonObj.optString("name"));

Trouble with Android JSONObject from Grails webservice

Okay I am quering data from a Grails webservice that returns JSON. The JSON when viewed with the JSONViewer app parses fine. When I take that same string and use JSONObject(string) in my Android app I get "value of String cannot be converted to JSONObject."
Here's my JSON string
[[{"class":"mygrails.TopTen","id":491,"ttAmount":14200000,"ttMlId":402,"ttRank":1,"ttWeekId":1108},{"class":"mygrails.MovieList","id":402,"mlApproved":1,"mlApprovedId":5,"mlMovieId":"GNOMEOAN","mlReleaseDate":"2011-03-08T07:41:45Z","mlTitle":"Gnomeo and Juliet","mlWeekId":1106}]]
Now the JSON is comes from the standard JSON conversion of a SQL data using render from the groovy file through the import grails.converters.JSON.
... //(call to render JSON in the groovy file)
def a
a = Table.findAll("from someTable as st where st.id=" params.id)
render a as JSON
...
So I am not sure what I doing wrong and why the JSON looks a little off to me. (still new to JSON)
In json if you see "[]" means its a json array and if you see "{}" it is an json object. Both of then can have the other nested inside then.
In your case the string the starts with json array.
So try something like the following
String str = "[[{"class":"mygrails.TopTen","id":491,"ttAmount":14200000,"ttMlId":402,"ttRank":1,"ttWeekId":1108},{"class":"mygrails.MovieList","id":402,"mlApproved":1,"mlApprovedId":5,"mlMovieId":"GNOMEOAN","mlReleaseDate":"2011-03-08T07:41:45Z","mlTitle":"Gnomeo and Juliet","mlWeekId":1106}]]";
JSONArray jsonArray = new JSONArray(str);
jsonArray = jsonArray.getJSONArray(0);
JSONObject jsonObject = jsonArray.getJSONObject(0);
String class = jsonObject.getString("class"); // class will value "mygrails.TopTen"
Try to create an JSONArray from the String instead of JSONObject. I didn't test this but that should do the trick: you have two nested arrays that contain then actual data.
Check out your JSON online with http://jsonformat.com/
http://www.freeformatter.com/json-formatter.html
JSON Viewer
http://jsonviewer.stack.hu/
Paste your text in there and you can see what you should parse:

Categories

Resources