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);
Related
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");
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
Hello i am getting error in the follwing code as- "Value at reachus of type java.lang.String cannot be converted to JSONObject"..I am getting values for all other Json object but getting error at reachus object..how to convert java.lang.String to the JsonObject?please give the solution..Thanks
JSONObject jObj = new JSONObject(Constants.sercall.response_str);
JSONObject jObj2 = new JSONObject;
jObj2 = jObj.getJSONObject("value");
JSONObject jObj6=new JSONObject;
jObj6=jObj2.getJSONObject("reachus");
if (jObj6.has("Email")) {
activitycontactinfo = jObj6.getString("Email");
activitycontactinfo = URLDecoder.decode(activitycontactinfo);
activitycontactinfo= activitycontactinfo.replace("%20", " ");
activitycontactinfo = Constants.convertToUpperCase(activitycontactinfo);
}
Here is my Json:
{"value":{
"classdetails":{},
"other_activities_details":"",
"activitydetails":{},
"youlearn":"",
"reachus":{
"Email":[
"varsha.b%40wiztango.com"
],
"facebook":"http%253A%252F%252Fwww.facebook.com%252FVarsha%2520Borhade",
"website":"http%253A%252F%252Fwww.wiztango.com",
"linkedin":"http%253A%252F%252Fwww.linkedin.com%252Fborhadevarsha",
"twitter":"http%253A%252F%252Fwww.twitter.com%252Fvarshaborhade",
"phone number":[
{
"value":"678698",
"meta":"varsha"
}
]
},
"spons_speak_host":"",
"categories":"",
"display_blocks":[],
"display_user_information":[],
"display_content_information":[],
"display_published_contents":[],
"faculty_published_contents":[],
"open_url_enrollment_users_list":"0",
"total_participants":[],
"display_agenda_information":[],
"faculty_published_agenda":[],
"display_published_agenda_ids":[]
}
}
This code enables you to access the 'reachus' field and email:--
Use this code:--
JSONObject jsonObject = new JSONObject(s);
JSONObject reachusJson = jsonObject.getJSONObject("value")
.getJSONObject("reachus");
JSONArray emailArray = reachusJson.getJSONArray("Email");
Log.e("email ", emailArray.getString(0));
First, please use more meaningful names for your variables, then look at the structure of your JSON, it's:
JSONObject (main) -> JSONObject (value) -> JSONObject (reachus) -> JSONArray (Email) -> String
So:
Get your JSOBObject
JSONObject json = new JSONObject(Constants.sercall.response_str);
Get JSONObject for "value":
JSONObject valueJson = json.getJSONObject("value");
Get JSONObject for "reachus":
JSONObject reachusJson = json.getJSONObject("reachus");
Because "Email" is JSONArray, first get JSONArray, then get the first string (assuming there is one):
if (reachusJson.has("Email")) {
JSONArray emailJsonArray = reachusJson.getJSONArray("Email");
//rest of your code
activitycontactinfo = emailJsonArray.getString(0);
activitycontactinfo = URLDecoder.decode(activitycontactinfo);
activitycontactinfo = activitycontactinfo.replace("%20", " ");
activitycontactinfo = Constants.convertToUpperCase(activitycontactinfo);
}
You might want to add some checks in case Email array is empty.
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>
I'm trying to take the following string that I got as a json object:
[
{
"id": "picture1",
"caption": "sample caption",
"picname": "sample picture name"
}
]
and turn it into a array so I can populate a list
I've tried turning it into a jsonarray by doing this:
JSONArray myjsonarray = myjson.toJSONArray(string_containing_json_above);
but that didn't seem to work.
==============
Here is full code with the working solution
myjson = new JSONObject(temp);
String String_that_should_be_array = myjson.getString("piclist");
JSONArray myjsonarray = new JSONArray(String_that_should_be_array);
For(int i = 0; i < myjsonarray.length(); i++){
JSONObject tempJSONobj = myjsonarray.getJSONObject(i);
showToast(tempJSONobj.get("caption").toString());
}
temp is the json from the server
Issue is here:
JSONArray myjsonarray = myjson.toJSONArray(temparray);
Solution:
JSONArray myjsonarray = new JSONArray(myJSON);
// myJSON is String
Now here you are having JSONArray, iterate over it and prepare ArrayList of whatever types of you want.
here you get JSONArray so change
JSONArray myjsonarray = myjson.toJSONArray(temparray);
line as shown below
JSONArray jsonArray = new JSONArray(readlocationFeed);
and after
JSONArray jsonArray = new JSONArray(readlocationFeed);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject explrObject = jsonArray.getJSONObject(i);
explrObject.getString("caption");
}
JSONArray isn't working because the JSON you provided is not an array. You can read more about JSON syntax here: http://www.w3schools.com/json/json_syntax.asp
In the meantime, you could manually create your array by paring the JSON one string at a time.
JSONObject strings = new JSONObject(jsonString);
String array[] = new String[5];
if (jsonString.has("id"){
array[0] = jsonString.getString("id");
}
if (jsonString.has("caption"){
array[1] = jsonString.getString("caption");
}
...
etc.