I am new to android and stuck with some problem in Json parsing.
JSONArray resultJsonArray = data.getJSONArray("detailsArr");
getJSONArray is showing in red colour it means some issue is with getJSONArray. But I am not able to resolve it.
JSON:
{"msg":"","status":true,"result":[{"conversation":"<p>sani<\/p>","attachmentName":"","attachmentURL":"","clientType":"student","repliedOn":"30-Sep-15 11:19AM","expertName":"shubham goyal","expertPic":""},{"conversation":"<p>rere<\/p>","attachmentName":"","attachmentURL":"","clientType":"expert","repliedOn":"1-Oct-15 5:31PM","expertName":"shubham goyal","expertPic":""},{"conversation":"<p>all vl<\/p>","attachmentName":"","attachmentURL":"","clientType":"student","repliedOn":"1-Oct-15 5:44PM","expertName":"shubham goyal","expertPic":""},{"conversation":"<p>asa kk<\/p>","attachmentName":"","attachmentURL":"","clientType":"expert","repliedOn":"1-Oct-15 5:45PM","expertName":"shubham goyal","expertPic":""}]}
According to your JSON
it should be
JSONArray resultJsonArray = data.getJSONArray("result");
Since result is the name of the array.
I did't find detailsArr .
JSONObject reader = new JSONObject(Your_Json_Sring);
JSONArray jsonArray = reader.getJSONArray("result");
for (int i = 0; i < jsonArray.length(); i++)
{
JSONObject e = jsonArray.getJSONObject(i);
String conversation = e.getString("conversation");
}
Bundle doesn't have a method getJSONArray()
And your JSON string doesn't contain a key named detailsArr.
UPDATE:
I think what you want to do is this:
JSONArray resultJsonArray = new JSONObject(data.getString("detailsArr"))
.getJSONArray("result");
Try following things:
Check imports, it should be like (if you are using other library - should be similar)
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
Check if you data is type of JSONObject.
Step 1:
Using this Link Create one Pojo for your Json String.
Step 2:
put following in your gradle than sync your gradle file
compile 'com.google.code.gson:gson:2.4'
Step :3
and at Last Use following code for deserialized Json
Gson mGson = new Gson();
JsonReader reader = new JsonReader(new StringReader(YOUR_JSON_STRING));
final YOUR_POJO_CLASS_NAME mList = mGson.fromJson(reader, new TypeToken<YOUR_POJO_CLASS_NAME>() {
}.getType());
I hope your are clear with my solution.
Best of Luck
Related
How do I fetch this data using json. I have tried this but not working. Im very much confused about how to fetch the below data. Any help appreciated
{
"sftid": [
"sfta"
],
"todate": 205618.268,
"today": 5307.174,
"sfta": 5093.717,
"sftb": 213.457,
"sftc": 0,
"cropday": 21,
"crushingday": 21,
"vtractor": 4535.075,
"vtruck": 532.687,
"vbcart": 239.412,
"yestitons": 6374,
"ytractor": 248,
"ytruck": 90,
"ybcart": 40,
"ycart": 48,
"hr1": 515.043,
"hr2": 625.651,
"hr3": 706.789,
"hr4": 626.796,
"hr5": 630.639,
"hr6": 608.053,
"hr7": 604.559,
"hr8": 776.187
}
JSONObject jobject = new JSONObject(response);
JSONObject jsonObj = jobject.getJSONObject("todate");
You can do something like this
JSONObject jobject = new JSONObject(response);
then
double todate = jobject.getDouble("todate");
and so on as per the data type
or simply you can use http://www.jsonschema2pojo.org/ which will help you to convert your json data to a model class which you can use to fetch the data easily.
For this you need to use Gson library.
So suppose you created class with above link as "Myresponse" the you can use it as
Gson gson = new Gson();
Myresponse data = gson.fromJson("<your json resopnse>",Myresponse.class);
I have added screenshot for the settings you need to do on jsonschema2pojo website
As you see your response contains one JSONObject which contains many elements, First element is an JSONArray rest can be taken as a double/String.
It goes as follows :
JSONObject json = new JSONObject(response);
JSONArray sftid_array = json.getJSONArray("sftid");
ArrayList<String> sftid = new ArrayList();
for(int i = 0; i < sftid_array; i++) {
sfid.put(sftid_array.getString(i));
}
double todate = json.getDouble("todate");
double today = json.getDouble("today");
double sfta = json.getDouble("sfta");
double sftb = json.getDouble("sftb");
.
.
.
and so on
You can also use Array to store data of JSONArray. Don't forget to put it in try catch as it may throw JSONException.
You could try:
JSONObject jobject = new JSONObject(response.toString());
JSONObject jsonObj = jobject.getJSONObject("todate");
I want to store a particular value of JSONobject response in one variable.
My JSON response is a string:
{"username":"admin","password":"admin","mobile":"xxxxxxxxxx"}
And from this response I want to retrieve the mobile number.
Can Anyone help?
Your Json is
{"username":"admin","password":"admin","mobile":"xxxxxxxxxx"}
Parse it Easily
JSONObject jsonObject= new JSONObject(json_response);
String mobile= jsonObject.optString("mobile");
Use asynchttpclient library. Its easy to use and parse values.
add this line in app gradle,
compile 'com.loopj.android:android-async-http:1.4.9'
Then,
Let, String s = "{"username":"admin","password":"admin","mobile":"xxxxxxxxxx"}";
JSONObject jsonObject = new JSONObject(s);
String username = jsonObject.optString("username");
String mobile = jsonObject.optString("mobile");
String password = jsonObject.optString("password");
You need to do something like the below:
final JSONObject jsonObject = new JSONObject(response);
if (jsonObject.length() > 0) {
String mobile = jsonObject.optString("mobile");
Log.e("TAG", "mobile:"+mobile);
}
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!
This might be a simple question, so sorry for that. I'm trying to develop a radio app for my internet radio station. I'm using JSON format for the requests (Example JSON) to get information from my station. For example the "title" tag. I want to get this information.
My first try:
JSONObject data = response.getJSONObject("data");
songname.setText(data.getString("title"));
But the problem is that I cannot get this information. What can I do? Thanks in advance.
As your JSON do like this
JSONObject josnOBJ = new JSONObject(response);
JSONArray jArray = josnOBJ.getJSONArray("data");
JSONObject jsonData = null;
String title = null;
for (int i=0; i < jArray.length(); i++)
{
try {
jsonData = jArray.getJSONObject(i);
title= jsonData .optString("title");
} catch (JSONException e) {
// Oops
}
}
songname.setText(title);
You could use some generic JSON parsers instead of manual work. Eg: https://github.com/bajicdusko/AndroidJsonProvider
Just create model class regarding your json content. Check examples in README on github url.
Hope i could help.
You can use GSON library in case you have lot of values to fetch from JSON ,that would take care of everything for you.
I need to convert a JSONObject to a Location object using gson libraries for an android project of mine. I'm not sure on how to do this. Can anyone help me with this. Thanks in advance.
I have a code something like
JSONArray input = new JSONArray(extras.getString("loc_json"));
I wanted to convert the JSONObject taken from the JSONArray to a Location class object. I just wanted to know whether there is a function that does it directly.
Pardon me if I framed the question in a wrong way. Since I haven't got the direct function, I did it in this way.
loc_temp = (Location) gson.fromJson(input.getJSONObject(i).toString(), Location.class);;
Sorry for the stupid question.
Here's a tutorial for GSON - I think it should help bolster your understanding. Essentially the parsing is done like this:
String mJsonString = "...";
JsonParser parser = new JsonParser();
JsonElement mJson = parser.parse(mJsonString);
Gson gson = new Gson();
MyDataObject object = gson.fromJson(mJson, MyDataObject.class);
if you still want to use org.json.JSONObject:
org.json.JSONObject o;
ObjectMapper m = new ObjectMapper();
MyClass myClass = m.readValue(o.toString(), MyClass.class);
use com.google.gson.JsonObject & JsonArray instead of org.json.*
like this :
Gson gson = new Gson();
Type typeOfT = new TypeToken<List<Location.class>>(){}.getType();
JsonParser parser = new JsonParser();
JsonObject jo = (JsonObject) parser.parse(jsonStr);
JsonArray ja = jo.getAsJsonArray("memberName");
list = gson.fromJson(ja, typeOfT);
Convert JSONObject to Model class::
val jsonObject = JSONObject(data[0].toString())
val jsonModel = jsonObject.getJSONObject(KEY.message).toString()
val chatModel = Gson().fromJson(model, ChatModel::class.java))
For kotlin
val objType = object : TypeToken<MyClass>() {
}.getType()
var myclassObj = gson.fromJson(value,objType)
or
val objectResponse = Gson().fromJson(Gson().toJson(resp), MyClass::class.java)