How to send list of byte arrays to json - android

How i can pass ArrayList<byte[]> to JSONObject as a parameter?
For example, I need to send a Id (string), Name (string) and photoList (ArrayList<byte[]>) in a json object.

You can use GSON LIBRARY . The code you need can be as :
String jso;
jso = new Gson().toJson(photoList);
It should convert the arraylist(i.e. photoList) to jsonObject.

Just use GSONlib for this if you want to convert arraylist into json

If you are using GSON library use the following code to convert your ArrayList to JSON.
String jsonString = new Gson().toJson(yourArrayList);

Related

how to convert sqlite to json in android

How to Convert this
[{destLocId=10, createdUserId=b9ab2d71-9a69-4ba3-b498-d36446a154d6,
createdDate=2016-6-29 14:35:00}]
to this :
[{"destLocId":10, "createdUserId":b9ab2d71-9a69-4ba3-b498-d36446a154d6,
"createdDate":2016-6-29 14:35:00}]
You can use GSON library for converting ArrayList to JSON. Here an example,
ArrayList<String> bibo = new ArrayList<String>();
bibo.add("obj1");
bibo.add("obj2");
bibo.add("obj3");
String json = new Gson().toJson(bibo);
And this example from Gson User Guide to use it on collection.

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:

how to convert json object to string in android..?

I want to convert from JSONObject
{"CNo":80,"CName":"ganesh","CMail":"ganesh#ganesh.com","CMailType":"home","CPhNo":9878987776,"CPhNoType":"home","ClientNo":1}
to
{\"CNo\":80,\"CName\":\"ganesh\",\"CMail\":\"ganesh#ganesh.com\",\"CMailType\":\"home\",\"CPhNo\":9878987776,\"CPhNoType\":\"home\",\"ClientNo\":1}
Try to use toString() on your JSON object
I hope this helps. Just learnt it yesterday :)
JSONObject foo = yourJSONOject;
String uid = foo.get("CNo").isString().toString();
String type = foo.get("CName").isString().toString();
.
. //for each Key field.
I am not sure why you have put the escapes in the string, but you can call append() and get the OP as you want it.
Below code will convert the given JsonObject to string.
Gson gson = new Gson();
String bodyInStringFormat = gson.toJson(passYourJsonObjectHere);

Categories

Resources