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.
Related
I need some help to pass the jsonarray in retrofit post method in Android.
I have create one pojo class List<ReportList> when I call the api that time i convert this pojo class into JSONArray using Gson() like this
JsonArray myCustomArray = new Gson().toJsonTree(ReportsArray).getAsJsonArray();
and than I post this jsonArray into retrofit post method like this
#FormUrlEncoded
#POST(midURL)
Call<Json> ReportsManagement(#Field(fClass) String classs,
#Field(fMethod) String method,
#Field(fType) String type,
#Field(fUserID) String userID,
#Field(fDate) String Date,
#Field(fReportsArray) String ReportsArray,
#Field(fEntryByUser) String EntryByUser);
Now when I call the api that time I have show some issue in passing the json data in api in android studio logcat.
class=crud&method=ReportsManagement&Type=ADD&UserID=8&Date=2018-06-08&ReportsArray=[{"Date":"08-06-2018","IV1":"","IV10":"","IV11":"","IV12":"","IV13":"","IV14":"","IV15":"","IV16":"","IV17":"","IV18":"","IV19":"","IV2":"","IV20":"","IV21":"","IV22":"","IV23":"","IV24":"","IV25":"","IV26":"","IV3":"","IV4":"","IV5":"","IV6":"","IV7":"","IV8":"","IV9":"","IVF":""},{"Date":"08-06-2018","FIO2":"","IV1":"","IV10"%06-08 08:13:07.461 23291-23476/com.abc D/OkHttp:3A"","IV11":"","IV12":"","IV13":"","IV14":"","IV15":"","IV16":"","IV17":"","IV18":"","IV19":"","IV2":"","IV20":"","IV21":"","IV22":"","IV23":"","IV24":"","IV25":"","IV26":"","IV3":"","IV4":"","IV5":"","IV6":"","IV7":"","IV8":"","IV9":"","IVF":""}]
Error in json :%06-08 08:13:07.461 23291-23476/com.abc D/OkHttp:3A
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);
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");
suppose I have {"data": {"243232": {"id": "testid","name": "test" } }}
so how to get correct value thanks.
you cant directly pass above String with Gson as "243232" is number and we cant declare variable which start from number, So for parsing this you must modify the string with some notification
i.e. {"data": {"temp243232": {"id": "testid","name": "test" } }}
here i modified string manually "243232" by "temp243232", now you can parse it
You can use a JSON parser for Java like Google GSON. This works best if the class whose object your data in json represents, is known to you.
String str = ""; //contains your json string
Gson gson = new Gson();
MyClass var = gson.fromJson(str,MyClass.class);
where MyClass will be a class which represents the object.
Then you can access data just like you'd do from a data structure in your program, it'll be something like var.data.
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: