parsing does not work json Android - android

parsing does not work json Android
parsing does not work json Android
{
"group_name":"МБА-14",
"days":[
{
"weekday":1,
"lessons":[
{
"subject":"Научно-исследовательский семинар",
"type":0,
"time_start":"17:10",
"time_end":"18:30",
"time_number":6,
"parity":1
}
]
}
]
}
I need to get the value weekday. This is my code:
JSONObject jsonObject=new JSONObject(str);
JSONObject jsonObject1=jsonObject.getJSONObject("weekday");
Log.e("aaaa", jsonObject1.toString() );

JSONArray days = jsonObject.getJSONArray("days");
JSONObject oneDay = days.getJSONObject(0);
int weekday = oneDay.getInt("weekday");

If your "str" variable contains this,
then parse like
JSONObject jsonObject=new JSONObject(str);
JSONArray jArray = jsonObject.getJSONArray("days");
String weekday = (jArray .getJSONObject(0)).getString("weekday");

Related

Value at 0 cannot be converted to JSON Object

I am reading String data from SharedPreference. The format of my String is
[{
"name": "sdf",
"phone": "2356235623",
"no_people": "2"
}]
I want to read all JSONobjects present in this JSONArray. So I am converting this String to JSON Array as
String tempData = addqueuetemp.getString("tempdata", "");
JSONArray cacheTemp=new JSONArray(tempData);
Now I am parsing this JSON Array as
JSONArray cacheTemp=new JSONArray(tempQueue);
for(int i=0;i<cacheTempQueue.length();i++){
JSONObject tempObject=cacheTemp.getJSONObject(i);
CurrentStatusEntry tempEntry=new CurrentStatusEntry();
tempEntry.setName(tempObject.getString("name");
current.add(tempEntry);
adapter = new QueueAdapter(current, getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
}
But I am getting error as
org.json.JSONException: Value {"name":"sdf","phone":"2356235623","no_people":"2"} at 0 of type java.lang.String cannot be converted to JSONObject
How to resolve this ?
Use this
JSONArray cacheTemp=new JSONArray(tempQueue);
for(int i=0;i<cacheTemp.length();i++){
JSONObject tempObject=cacheTemp.getJSONObject(i);
CurrentStatusEntry tempEntry=new CurrentStatusEntry();
tempEntry.setName(tempObject.getString("name");
current.add(tempEntry);
adapter = new QueueAdapter(current, getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
}
try this
JSONObject obj1 = new JSONObject(result.toString());
JSONArray jsonArray = new JSONArray();
jsonArray.put(obj1);
JSONObject dataObj = new JSONObject();
dataObj.put("Data", jsonArray);
JSONArray Jarray = obj.getJSONArray("value");
Use cacheTemp JSONArray in for loop in place of cacheTempQueue and put your setAdateper code to outside loop.
JSONArray cacheTemp=new JSONArray(tempQueue);
for(int i=0;i<cacheTemp.length();i++){
JSONObject tempObject=cacheTemp.getJSONObject(i);
CurrentStatusEntry tempEntry=new CurrentStatusEntry();
tempEntry.setName(tempObject.getString("name");
current.add(tempEntry);
}
adapter = new QueueAdapter(current, getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);

How to parse JSON using hardocoded values

I am trying to parse json but it gives me exception.
I hardcoded expected json as String like this
String stringJSON="[{\"value1\":\"ABC567123\",\"end_at\":\"08/28/2014 09:10:00\",\"start_at\":\"04/25/2016 09:20:00\"}]";
Valid json is like this
[
{
"value1": "ABC567123",
"end_at": "08/28/2014 09:10:00",
"start_at": "04/25/2016 09:20:00"
}
]
Now I am trying to parse json like below and getting exception.
JSONObject responseObJ;
try {
responseObJ= new JSONObject(stringJSON); //error here
if(responseObJ!=null){
//do something
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
Please suggest what to do?
//hard coded it for temporary - json result is expected to exact same
stringJSON is contains JSONArray instead of JSONObject as root element in JSON String.
Either remove [] from start and end of String according to current code or if multiple JSONObject's is available in JSONArray then get JSONArray from stringJSON :
JSONArray responseObJ= new JSONArray(stringJSON);
[ ] they show that it has an array of objects in it so you can retrieve it like this
JSONArray jsonArray= new JSONArray(stringJSON);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jObject.getJSONObject(i);
// here you can get the values of objects stored in the jsonObject
}
In your case you have only one object so you don't have to use loop instead you can get it like this
JSONArray jsonArray= new JSONArray(stringJSON);
JSONObject jsonObject = jObject.getJSONObject(0);
yes as you said its valid json but its JsonArray not JsonObject.
Just remove [] from start and end.
your string should be
String stringJSON="{\"value1\":\"ABC567123\",\"end_at\":\"08/28/2014 09:10:00\",\"start_at\":\"04/25/2016 09:20:00\"}";
or if you want to work with current string then use JsonArray instead of JsonObject
JSONArray responseObJ= new JSONArray(stringJSON);
Can you try Deserialize method of ScriptSerializer class? Like:
var scriptSerializer = new JavaScriptSerializer();
var obj = scriptSerializer.Deserialize<Object>(str);

Android parse JSON, how to fetch only single parameter

hi i have only one parameter to parse
this is what i have tried....
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray("result");
and here is the below json
{"result" : "169489465811879423"}
Use this
JSONObject jsonObject = new JSONObject(response);
String result = jsonObject.getString("result");
Here there is no JsonArray and you are trying to get an array

How to call the JSON object in Android

I am new to the android development. i am having the following android code to call the json
try {
JSONObject jsonObject = new JSONObject(result);
//JSONObject object = jsonObject.getJSONObject("CUSTOMER_ID");
JSONArray jArray = new JSONArray(CUSTOMER_ID);
returnUsername1 = jArray.getInt("CUSTOMER_ID");
Toast.makeText(getApplicationContext(), ""+returnUsername1,Toast.LENGTH_LONG).show();
for (int i = 0; i < jArray.length(); i++) {
}
My JSON format is like [[{"0":"1","CUSTOMER_ID":"1"}]].
i refer some json format it should like [{"0":"1","sno":"1"}] i can understand this.But mine is different from this.
how can i call the customer_id using the above code.anyone can suggest a solution.
What you have is a Json Array
JSONArray jsonarray = new JSONArray(result); // result is a Array
[ represents json array node
{ represents json object node
Your Json. Do you need a Json array twice?
[ // array
[ //array
{ // object
"0": "1",
"CUSTOMER_ID": "1"
}
]
]
Parsing
JSONArray jsonArray = new JSONArray(result);
JSONArray ja= (JSONArray) jsonArray.get(0);
JSONObject jb = (JSONObject) ja.get(0);
String firstvalue = jb.getString("0");
String secondvalue = jb.getString("CUSTOMER_ID");
Log.i("first value is",firstvalue);
Log.i("second value is",secondvalue);
LogCat
07-22 14:37:02.990: I/first value is(6041): 1
07-22 14:37:03.048: I/second value is(6041): 1
Generally, to get a JSONObject from a JSONArray:
JSONObject jsonObject = jsonArray.getJSONObject(0); //0 -> first object
And then
int userID = jsonObject.getInt("CUSTOMER_ID");
CUSTOMER_ID is not considered a JSONObject in this case. If jsonObject is what you think it is, then you should be able to access it with jsonObject.getString("CUSTOMER_ID")
if you have any problems regarding to your JSON format first validate it through this site
http://jsonlint.com/
then for parsing
JSONObject jsonObject = new JSONObject(result);
// since your value for CUSTOMER_ID in your json text is string then you should get it as
// string and then convert to an int
returnUsername1 = Integer.parseInt(jsonObject.getString("CUSTOMER_ID"));
Try this
JSONObject jsonObject = new JSONObject(result);
JSONArray jArray =json.getJSONArray("enterjsonarraynamehere");
for (int i=0; i < jArray.length(); i++)
{
try {
JSONObject oneObject = jArray.getJSONObject(i);
// Pulling items from the array
String cust= oneObject.getString("CUSTOMER_ID");
} catch (JSONException e) {
// Oops something went wrong
}
}
Im assuming your json is something like this
<somecode>
{
"enterjsonarraynamehere": [
{ "0":"1",
"CUSTOMER_ID":"1"
},
{ "0":"2",
"CUSTOMER_ID":"2"
}
],
"count": 2
}
<somecode>

Simple Json parsing in android

I got the Json response. I am not able to get the values from the string.my string is
Json_response is
{"NameAllList":[{"Current":{"time":"2012-02-21T08:04:21","Name":"abcd"},
"Next":{"Name":"data1","StartTime":"2012-02-21T08:06:21"}},{"Current":{"time":"2012-02-21T08:14:21","Name":"defg"},
"Next":{"Name":"data2","StartTime":"2012-02-21T08:24:21"}},{"Current":{"time":"2012-02-21T08:28:21","Name":"ghij"},
"Next":{"Name":"data3","StartTime":"2012-02-21T08:34:21"}},{"Current":{"time":"2012-02-21T08:40:21","Name":"knmo"},
"Next":{"Name":"data4","StartTime":"2012-02-21T08:48:21"}}]}
and i tried this.
JSONObject jsonObj = new JSONObject(json_response);
JSONObject subObj = jsonObj.getJSONObject("Current");
String name_current =subObj.getString("Name");
but i am not able to get the value of "Name". what mistake i have done. provide the link to do the above parsing.
first of all, your JSON response is having NameAllList as a JSON Array of objects.
So you have to fetch JSON Array first, then you can fetch one-by-one object.
for example:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
for(int i=0; i<array.length(); i++)
{
// Retrieve Current object as such
JSONObject objCurrent = array.getJSONObject("Current");
// Retrieve Next object as such
JSONObject objNext = array.getJSONObject("Next");
}
You are not parsing json properly, so you are not able to fetch value of Name. Please note JSON Annotation [] represent JSONArray, and {} respresent JSONObject, so method to get current item's name is:
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArr=jsonObj.getJSONArray("NameAllList");
String Hora_name_current="";
for(int i=0;i<jsonArr.length();i++)
{
JSONObject obj=jsonArr.get(i);
try{
JSONObject subObj = obj.getJSONObject("Current");
Hora_name_current =subObj.getString("Name");
break;
}catch(JSONException ex)
{
}
}
looks like you're trying to use JSONObject when you should be using JSONArray for the second request. Try this:
JSONObject jsonString = (new JSONObject(json_response_string));
JSONArray array = jsonString.getJSONArray("NameAllList");
In your JSON return, "NameAllList is actually an array and needs to be handled as such. Once you set it to "array", you can then run a for loop and treat it like any other array in Java.
Let me know if that helps.
David
JSONObject jsonObj = new JSONObject(json_response_string);
JSONArray jsonArray = jsonObj.getJSONArrays("NameAllList");

Categories

Resources