In my code I'm getting an array using a for loop. The array contains four values. How do I print the values in a textview? I want to print these four values in four textviews.
My Code:
HttpClient client2 = new DefaultHttpClient();
HttpConnectionParams.setConnectionTimeout(client2.getParams(), 15000);
HttpConnectionParams.setSoTimeout(client2.getParams(), 15000);
HttpUriRequest request2 = new HttpGet(SelectMenuAPI2);
HttpResponse response2 = client2.execute(request2);
InputStream atomInputStream2 = response2.getEntity().getContent();
BufferedReader in2 = new BufferedReader(new InputStreamReader(atomInputStream2));
String line2;
String str2 = "";
while ((line2 = in2.readLine()) != null) {
str2 += line2;
}
JSONObject json3 = new JSONObject(str2);
// message = json2.getString("message");
status = json3.getString("status");
if (status.equals("1")) {
JSONArray school2 = json3.getJSONArray("data");
for (int i = 0; i < school2.length(); i++) {
JSONObject object3 = school2.getJSONObject(i);
Elementarytxt.settext("");
Middle.setText("");
High.setText("");
Atypical.setText("");
}
}
My JSON Data:
{
"status":1,
"data":[
{"title":"Elementary"},
{"title":"Middle"},
{"title":"High"},
{"title":"Atypical"}
]
}
JSONArray jsonArray = jObject.getJSONArray(ARRAYNAME);
if (jObject != null) {
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
txt.setttext(jsonObject.getString(CREATEDDATE));
txt.setttext(jsonObject.getString(SUBJECT));
}
}
Please check this. It might help you...
String [] mVal = new String[school2.length()];
for (int i = 0; i < school2.length(); i++)
{
mVal[i] =school2.getJSONObject(i).getString("title");
}
Elementarytxt.setText(mVal[0]);
Middle.setText(mVal[1]);
High.setText(mVal[2]);
Atypical.setText(mVal[3]);
I hope This will Help you..
String[] vals=new String[4];
for (int i = 0; i < school2.length(); i++) {
JSONObject object3 = school2.getJSONObject(i);
vals[i]=object3.getString("title");
}
Elementarytxt.settext(vals[0]);
Middle.setText(vals[1]);
High.setText(vals[2]);
Atypical.setText(vals[3]);
Related
I have 3 records in my database but only one shows. No idea how to fix the problem, I tried everything. HELP
public void onResponse(JSONArray response) {
JSONObject jo;
Teacher teacher;
try {
for (int i = 0; i < response.length(); i++) {
JSONArray innerJsonArray = response.getJSONArray(i);
jo = innerJsonArray.getJSONObject(i);
int id = jo.getInt("id");
String name = jo.getString("teacher_name");
String description = jo.getString("teacher_description");
String imageUrl = jo.getString("teacher_image_url");
teacher = new Teacher(name, description, PHP_MYSQL_SITE_URL+imageUrl);
teachers.add(teacher);
te.setText(Integer.toString(response.length()));
}
//SET SPINNER
adapter = new ListViewAdapter(c, teachers);
gv.setAdapter(adapter);
there should be 2 for loops , one for JSONArray innerJsonArray = response.getJSONArray(i); and second for JSONArray innerJsonArray = response.getJSONArray(j); that iterates through innerJsonArray
public void onResponse(JSONArray response) {
JSONObject jo;
Teacher teacher;
try {
for (int i = 0; i < response.length(); i++) {
JSONArray innerJsonArray = response.getJSONArray(i);
for (int j = 0; j < innerJsonArray.length(); j++) {
jo = innerJsonArray.getJSONObject(j);
int id = jo.getInt("id");
String name = jo.getString("teacher_name");
String description = jo.getString("teacher_description");
String imageUrl = jo.getString("teacher_image_url");
teacher = new Teacher(name, description, PHP_MYSQL_SITE_URL + imageUrl);
teachers.add(teacher);
te.setText(Integer.toString(response.length()));
}
}
//SET SPINNER
adapter = new ListViewAdapter(c, teachers);
gv.setAdapter(adapter);
}
}
I have a json which has multiple values separated by commas which is in the
form of array.I want to get skills and platforms string separately.Can you please help me?
I want to show skills string and platforms string in text.
Please help me.
The format is:
{
"data": [
{
"skills": "ANDROID SDK, ANIMATION, ANGULARJS,",
"platforms": "IOS Application, Social Networking, Online shopping Sites, Web Application"
}
],
"status": 100
}
Try this one
try {
JSONObject ob = new JSONObject(response);
int status = ob.getInt("status");
if (status == 100) {
JSONArray ja = ob.getJSONArray("data");
for (int i = 0; i < ja.length(); i++) {
values = new HashMap<>();
JSONObject vj = ja.getJSONObject(i);
String skills = vj.getString("skills "));
String platforms=vj.getString("platforms"));
data.add(values);
}
split these 2 strings using 'split()'
List<String> skillsArray = Arrays.asList(skills.split(","));
List<String> platformsArray= Arrays.asList(platforms.split(","));
String in = "your json";
JSONObject jsonObj = new JSONObject(in);
// Getting JSON Array node
JSONArray jsonarray = jsonObj.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String skills = jsonobject.getString("skills");
String platforms = jsonobject.getString("platforms");
}
Try this
JSONObject jsonObject = new JSONObject("Your json response");
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String skill = jsonobject.getString("skills");
String platforms = jsonobject.getString("platforms");
String[] skillsArray = skill.split(Pattern.quote(","));
String[] platformsArray = platforms.split(Pattern.quote(","));
for (int j=0; j<skillsArray.length; j++)
{
Log.i("Skill Value ", "=" + skillsArray[j]);
}
for (int j=0; j<platformsArray.length; j++)
{
Log.i("platforms Value ", "=" + platformsArray[j]);
}
}
Output
First of all convert your output string into json. Note that output string is the string which contains your results of json format that you have posted above.following is the code on how to do that:
JSONObject myJsonResponse = new JSONObject(yourString);
The next one is Array. That is how you will get it and will iterate over it.
JSONArray jsonarray = jsonObj.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject innerJsonObject= jsonarray.getJSONObject(i);
String skills = innerJsonObject.getString("skills");
String platforms = innerJsonObject.getString("platforms");
}
Now you have gotten your required fields and you can now perform any String functions over the String skills and platforms.
Happy coding. .
The best way is creating a model class such :
public class MyModel{
Properties [] data;
class Properties{
public String skills;
public String platforms;
}
}
then you can parse your string to model with Gson library like this :
MyModel myModel = new Gson().fromJson(yourString, MyModel.class)
so all data is in myModel object and you can access to skills with
myModel.data[0].skills
to add Gson library to your project add below to your app gradle file :
compile 'com.google.code.gson:gson:2.8.1'
You may try this,
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject("JSON_STRING");
JSONArray jsonArray = jsonObject.getJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonobject = jsonArray.getJSONObject(i);
String skills = jsonobject.getString("skills");
String[] seperateData = skills.split(Pattern.quote(","));
for (int j = 0; j < seperateData.length; j++) {
Log.e("Your Skill Value-> ", seperateData[j]);
}
String platforms = jsonobject.getString("platforms");
seperateData = platforms.split(Pattern.quote(","));
for (int j = 0; j < seperateData.length; j++) {
Log.e("Your Platform Value-> ", seperateData[j]);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
class Data{
public String skills;
public String platforms;
}
Gson gson = new Gson();
JSONArray dataArray = response.getJSONArray("data");
List<Data> dataList= gson.fromJson(dataArray toString(), new TypeToken<List<Data>>() {
}.getType());
Try this.. It will make parsing easier.
implementation 'com.google.code.gson:gson:2.8.0'
First Parse your JSON:
JSONObject jsonObj = new JSONObject(in);
JSONArray jsonarray = jsonObj.getJSONArray("data");
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String skills = jsonobject.getString("skills");
String platforms = jsonobject.getString("platforms");
}
Then Split your string values:
String skills = "ANDROID SDK, ANIMATION, ANGULARJS";
String[] separated = CurrentString.split(",");
separated[0]; // this will contain "ANDROID SDK"
separated[1]; // this will contain " ANIMATION"
separated[2]; // this will contain " ANGULARJS"
You have to remove the space to the second String:
separated[1] = separated[1].trim();
Try this you will be getting all values of sapereted with commas
jsonString = getting string from server side
String[] separated = jsonString.split(",");
StringBuilder s = new StringBuilder(10000);
for (int i = 0; i < separated.length; i++) {
if (i == separated.length - 1) {
s.append(separated[i].trim() + "");
} else {
s.append(separated[i].trim() + ",\n");
}
}
//tvDisplayAddress.setText(s);
Log.e("VALUES: ",s+"");
Please help me to parse the following JSON data given below:
{
posts: [
{
count: 1,
user_id: "1",
name: "Dave Greeneberg",
email: "daveneberg#example.com",
profile_photo: "http://phontest.lbch.com//users/user1.jpg",
contest_count: "3",
photo_count: 19,
win_count: "0",
photos: [
"images/contest/diwali1.jpg",
"images/contest/diwalc2.jpg",
"images/contest/145043cd811.png",
"images/contest/145043def03411.jpg",
"images/contest/14504ger11.jpg"
]
}
]
}
I tried the following code but values in arrayList_ph is null. I am confused about how to parse this JSON content.
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = arr.getJSONObject(0).getString("name");
user_email = arr.getJSONObject(0).getString("email");
user_profile = arr.getJSONObject(0).getString("profile_photo");
user_count = arr.getJSONObject(0).getString("count");
user_photo_count = arr.getJSONObject(0).getInt("photo_count");
contest_count = arr.getJSONObject(0).getString("contest_count");
win_count = arr.getJSONObject(0).getString("win_count");
JSONArray ph_arr= arr.getJSONObject(0).getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
arrayList_ph.add(ph_arr.getString(in));
}
}
Please help me to parse that field.
Please try this solution.
This solutions is worked for me.
JSONObject object = new JSONObject("");
JSONArray arr = object.optJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.optJSONObject(index);
user = object1.optString("name");
user_email = object1.optString("email");
user_profile = object1.optString("profile_photo");
user_count = object1.optString("count");
user_photo_count = object1.optInt("photo_count");
contest_count = object1.optString("contest_count");
win_count = object1.optString("win_count");
JSONArray ph_arr = object1.optJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.opt(in).toString();
arrayList_ph.add(str);
}
}
change your parsing code to this ,
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts");
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = object1.getString("name");
user_email = object1.getString("email");
user_profile = object1.getString("profile_photo");
user_count = object1.getString("count");
user_photo_count = object1.getInt("photo_count");
contest_count = object1.getString("contest_count");
win_count = object1.getString("win_count");
JSONArray ph_arr= object1.getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.get(in).toString();
arrayList_ph.add(str);
}
}
Try -
JSONObject object = new JSONObject(json);
JSONArray arr = object.getJSONArray("posts”);
for (int index = 0; index < arr.length(); index++) {
JSONObject object1 = arr.getJSONObject(index);
user = object1.getString("name");
user_email = object1.getString("email");
user_profile = object1.getString("profile_photo");
user_count = object1.getInt("count");
user_photo_count = object1.getInt("photo_count");
contest_count = object1.getString("contest_count");
win_count = object1.getString("win_count");
JSONArray photos_arr= object1.getJSONArray("photos");
for (int in = 0; in < ph_arr.length(); in++) {
String str = ph_arr.get(in).toString();
arrayList_ph.add(str);
}
}
Please cross check Following:
1) I think your json response is not properly formatted.
Validate from this: https://jsonformatter.curiousconcept.com/
Your json response should be :
{
"posts":[
{
"count":1,
"user_id":"1",
"name":"Dave Greeneberg",
"email":"daveneberg#example.com",
"profile_photo":"http://phontest.lbch.com//users/user1.jpg",
"contest_count":"3",
"photo_count":19,
"win_count":"0",
"photos":[
{
"image":"images/contest/diwali1.jpg"
},
{
"image":"images/contest/diwalc2.jpg"
},
{
"image":"images/contest/145043cd811.png"
},
{
"image":"images/contest/145043def03411.jpg"
},
{
"image":"images/contest/14504ger11.jpg"
}
]
}
]
}
2) If that still does not work...Try debugging and post your log please...
how I can access data from a JSONArray? It is this and contains this information:
"deadlines": [
{
"start": 1439539200,
"end": 1439542800
},
{
"start": 1440144000,
"end": 1440147600
},
{
"start": 0,
"end": 0
}
]
I need to have in a String tag each item "start". Thanks
EDIT
My code is this:
JSONArray array = moduleObject.specialForcedConf;
// array = [{"deadlines":[{"start":1439539200,"end":1439542800},{"start":1440144000,"end":1440147600},{"start":0,"end":0}]}]
for (int j=0; j < array.length(); j++)
{
try
{
JSONObject obj = array.getJSONObject(j);
String start = obj.getString("start");
String end = obj.getString("end");
Log.e("", "start = " + start);
}
catch (JSONException e)
{
Log.e("", "error = " + e.getMessage());
}
}
I get this error:
"error = No value for start"
Do this
JSONArray array = moduleObject.specialForcedConf;
// array = [{"deadlines":[{"start":1439539200,"end":1439542800},{"start":1440144000,"end":1440147600},{"start":0,"end":0}]}]
JSONArray jArray = array.getJSONObject(0).getJSONArray("deadlines");
for (int i = 0; i < jArray.length(); i++) // assuming your array is jArray
{
try
{
JSONObject obj = jArray.getJSONObject(i);
String start= obj.getString("start"); // store in an ArrayList
String end = obj.getString("end"); //// store in an ArrayList
}
catch (JSONException e)
{
// Error
}
}
JSONArray mJsonArray=new JSONArray("Deadlines");
for(int i=0;i<mJsonArray.length();i++){
JSONObject mJsonObject=new JSONObject(mJsonArray.get(i).toString));
String start = mJsonObject.optString("start","");
String end = mJsonObject.optString("end","");
}
Try this:
StringBuilder sb = new StringBuilder();
JSONArray arr = new JSONArray("deadlines");
for(int i=0;i<arr.length;i++){
JSONObject obj = arr.getJSONObject(i);
sb.append(obj.get("start").toString());
sb.append(",");
}
String strStartTag = sb;
JSONArray ja = new JSONArray(yourjsondata));
for (int i = 0; i < ja.length(); i++) {
JSONObject jo_feed = new JSONObject(ja.get(i).toString());
String start = jo_feed.getString("start");
}
I cannot figure out why my parsing is not working, this is my JSON:
{
"fileVersion":"1.0",
"graves":[
{
"ID_grave":"1",
"ID_line":"1",
"sequence":"1",
"persons":[
{
"ID_person":"1",
"name":"Janez",
"surname":"Novak",
"dateBirth":"1956-08-11",
"dateDeath":"2014-02-12",
"important":"0",
"imp_desc":""
}
]
},
{
"ID_grave":"2",
"ID_line":"1",
"sequence":"2",
"persons":[
{
"ID_person":"2",
"name":"Mojca",
"surname":"Novak",
"dateBirth":"1953-02-13",
"dateDeath":"2012-04-08",
"important":"0",
"imp_desc":""
}
]
}
]
}
This code is working, when I want to get the first JSONObject:
String jsonData = convertStreamToString(in);
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
lineArrayList.add(grave.getString("ID_line"));
graveArrayList.add(grave.getString("ID_grave"));
}
But I would like to get the "persons" array in "graves" object. This should work but it's not, I am getting only the first persons array, where the name is Janez and not the second array where the name is Mojca:
String jsonData = convertStreamToString(in);
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.getJSONArray("persons");
for (int k = 0; k < persons.length(); k++) {
//The problem was because of the index i, you have to change to k and it will work
JSONObject grave = persons.getJSONObject(i);
nameArrayList.add(grave.getString("name"));
surnameArrayList.add(grave.getString("surname"));
}
}
graves is a JSONArray and persons is a JSONArray into graves
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.optJSONArray("persons");
if (persons != null) {
for (int j = 0; j < persons.length(); j++) {
}
}
}
Do your parsing as follows ,
String jsonData = convertStreamToString(in);
JSONObject json = new JSONObject(jsonData);
JSONArray name = json.getJSONArray("graves");
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.getJSONArray("persons");
for (int k = 0; k < persons.length(); k++) {
JSONObject grave = persons.getJSONObject(i);
nameArrayList.add(grave.getString("name"));
surnameArrayList.add(grave.getString("surname"));
}
}
Try This:
for (int i = 0; i < name.length(); i++) {
JSONObject grave = name.getJSONObject(i);
JSONArray persons = grave.optJSONArray("persons");
if (persons != null) {
for (int j = 0; j < persons.length(); j++) {
JSONObject grave= persons.getJSONObject(i);
lineArrayList.add(grave.getString("ID_line"));
//so on..
}
}
Ok only today I discovered that I am getting the data only from the first persons array and from the second where for example is a person with a name Mojca..I tried all the three given solutions but nothing works..