I know how to access to json. Now I get a json response like that:
"2014-02-16T20:27:54+00:00"
https://openligadb-json.heroku.com/api/last_change_date_by_league_saison?league_shortcut=bl1&league_saison=2013
this is not a JSONArray and has no Name. How can I access it?
It is not json formatted data. If you need it in json, you can generate it by yourself like this :
JSONObject json = new JSONObject();
json.put("data", "2014-02-16T20:27:54+00:00" );
json.toString(); // { "data" : ""2014-02-16T20:27:54+00:00" } it is json
In other case you can work with this data as typical String. It depends on what you need to achieve.
Good luck!
Related
What is the best way to cast my response from the server ?
By directly assigning the response to the entity
Ex: authorityStatusWithWorkFlowEntity = response.body();
OR
First Catching it as JsonElement OR JsonArray OR JsonObject and then converting it to specified entity?
Ex: lineEstimationEntity = new Gson().fromJson(response.body().getAsJsonObject(), new TypeToken<LineEstimationEntity>() {
}.getType());
In performance wise which is the best practice?
I'm using volley and trying to make request to API that I'm working on. Json Request should be like this format.
{
"name": "API name",
"param":{
"email": "user#mail.com",
"password": "password"
}
}
I've tried to use hashmap but I don't know how to put
<string string>
<string, hashmap>
this is getting complicated.
Now, how should I put these values in hashmap and convert it to JSONObject and send request to server.
If this is not the way it should be done, then what should I use instead?
HahMap can not be used for JSON serialization, I suggest you to use org.json (https://mvnrepository.com/artifact/org.json/json)
Ex:
JSONObject jsonObj = new JSONObject();
JSONObject param = new JSONObject();
param.put("email","blhablah");
param.put("password","blhablah");
jsonObj.put("name", "apiName");
jsonObj.put("param", param);
System.out.println(jsonObj.toString());
This will give you the json, like follwoing :
{"param":{"password":"blhablah","email":"blhablah"},"name":"apiName"}
I'm wondering what is the difference between using a JSONObject and JSONTokener to parse a JSON string.
I have seen examples where either of these classes were used to parse a JSON string.
For example, I parsed a JSON string like this:
JSONObject object = (JSONObject) new JSONTokener(jsonString).nextValue();
return object.getJSONObject("data").getJSONArray("translations").
getJSONObject(0).getString("translatedText");
But I also see examples where they simply use a JSONObject class (without using JSONTokener) to parse it:
String in;
JSONObject reader = new JSONObject(in);
JSONObject sys = reader.getJSONObject("sys");
country = sys.getString("country");
I read the description on this page, but still don't really get the difference. Is one better than the other performance wise and when should a JSONTokener be used?
Thanks!
Im using JsonObject to parse a response from an API, the problem is that the resulting String have double quotes, how to get string with no quotes?
JsonElement jelement = new JsonParser().parse(response);
JsonObject jobject = jelement.getAsJsonObject();
String authorization = jobject.get("authorization").toString();
Log.d("mensa","parseado jwt :"+authorization);
so my response looks like...
parseado jwt :"eyJ0eXAiOiJKV1QiLCJhbGciO..."
Actual Json response
{
"authorization": "eyJ0eXAiOiJKV1..."
}
what is the right way to parse this string so i dont get it wrapped with double quotes?
I believe you are using GSON library. No need to trim out your result, gson provide methods for that. try this
String authorization = jobject.get("authorization").getAsString();
Refer Gson API Doc : JsonElement
You are getting Object from Json and then converting to String. jobject.get("authorization").toString(); try to get String.
String authorization = jobject.getString("authorization");
Just trim out the quotes: authorization = authorization.substring(1, authorization.length() - 1)
Edit: You should actually use the getString() method as it will return the actual content as a string, however, the above still works as a quick workaround.
I am hoping someone might be able to help me out.
I have a Django server that is returning JSON to an iOS application. On the Django server, we are using
return HttpResponse(json.dumps(session_dict),mime_type)
to return the JSON to the client as (via Wireshark)
2f
{"session": "bcb493fb21ae8fcd9152e1924b3e5d9a"}
0
This response is somehow valid to the iOS application able to be parsed by the iOS JSON client libraries. This does not look like valid Json to me so I am surprised it works.
However, if I use the following in Android, I get an error:
Value session of type java.lang.String cannot be converted to JSONObject.
jsonObjSend.put("username", strUserName);
jsonObjSend.put("password", strPassword);
Add a nested JSONObject (e.g. for header information)
JSONObject header = new JSONObject();
header.put("deviceType","Android"); // Device type
header.put("deviceVersion","2.0"); // Device OS version
header.put("language", "es-es");
jsonObjSend.put("header", header);
// Output the JSON object we're sending to Logcat:
Log.i(TAG, jsonObjSend.toString(2));
} catch (JSONException e) {
e.printStackTrace();
}
try {
// Send the HttpPostRequest and receive a JSONObject in return
JSONObject jsonObjRecv = HttpClient.SendHttpPost(URL, jsonObjSend);
String sessionId = jsonObjRecv.getString("session");
Any suggestions?
Thank you,
Greg
Does your HttpClient.sendHttpPost method return a JSONObject? Not sure if it parses the response body from the HTTP POST into a JSONObject automatically. If it doesn't, then you would have to do that using the JSONTokener or use a library like Gson.
google's GSON may be a better choice you can convert an instance to json string directly such as
User user =new User("tom","12");
Gson gson =new Gson();
json=gson.toJson(user);