why it only shows the latest data from the json array - android

These are my JSON data:
[
{"haberBaslik":"ekrem kimdir?"},
{"haberBaslik":"doğa kimdir?"},
{"haberBaslik":"biz kimiz?"},
{"haberBaslik":"fatih naptı?"}
]
and this code only shows the latest data, but I need to show my all data
JSONArray jsonarray = new JSONArray(s);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String Haberbaslik = jsonobject.getString("haberBaslik");
tv1.setText(Haberbaslik);
}

JSONArray jsonarray = new JSONArray(s);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String Haberbaslik = jsonobject.getString("haberBaslik");
tv1.setText(tv1.getText()+ " "+Haberbaslik);
}

Related

Read JSON array without curly brackets

I'm trying to get the values from array in a JSON file. Array only has a square brackets and not curly brackets something like this:
"{"name_of_the_array: ["value_1", "value_2"]"}"
I found here some answer but it did not work for me the code of the answer is:
JSONArray array = new JSONArray("array_name");
for (int i = 0; i < array.length(); i++) {
str = array.get(i).toString();
list.add(str);
}
the list is ArrayList list.
{} denotes a JSONObject so you have to create a JSONObject first. [] denotes a JSONArray. You have to fetch the JSONArray from JSONObject with key.
Use this code
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray array = jsonObject.getJSONArray("array_name");
for (int i = 0; i < array.length(); i++) {
str = array.get(i).toString();
list.add(str);
}
Check this and this for parsing
try something like this
JSONArray array = new JSONArray("array_name");
for (int i = 0; i < array.length(); i++) {
str = array.getJSONObject(i).getString("string_name");
list.add(str);
}
or you can try this too
JSONArray array = new JSONArray("array_name");
for (int i = 0; i < array.length(); i++) {
str = array.getString("string_name");
list.add(str);
}

JSON Pasing - how to?

I don't know how to parse a JSON if start with jsonArray instead jsonObject.
There's the JSON code.
[
{
"id": 1,
"title":
{
"rendered": "Apple apologises and fixes security flaw"
}
},
{
"id": 2,
"title":
{
"rendered": "Trophy hunting removes good genes and raises extinction risk"
}
}
...
]
I don't know how to get the JSONArray length. such as:
for (int i = 0; i < JSONArray.length(); i++)
{
JSONObject JSONObject1 = JSONArray.getJSONObject(i);
int id = JSONObject1.getInt("id");
string title = JSONObject1.getString("rendered");
}
Any help will be greatly appreciated!
Assuming that your json value is in this string variable : json
String json = "your_json_data";
JSONArray jsonarray = new JSONArray(json);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObject = jsonarray.getJSONObject(i);
int id = jsonObject.getInt("id");
JSONObject titleObject = jsonObject.getJSONObject("title");
String rendered = titleObject.getString("rendered");
}
Try below code
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonObject= jsonarray.getJSONObject(i);
int id = jsonObject.getInt("id");
JSONObject jsonInner= jsonObject.getJSONObject("title");
string title = jsonInner.getString("rendered");
}

Android JSON getJSONObject not working, incorrect parsing

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..

android org.json.JSONArray cannot be converted to JSONObject?

[{"Episode Detail":[{"Episode-image":"http:\/\/app.lionforge.com\/comics\/adminpanel\/episode_image\/014929Quincredible_1-6.png","Episode-description":" dsdsdsdsds ","Episode-video":"http:\/\/www.youtube.com\/embed\/KhXTLAdadlw"},{"Episode-image":"http:\/\/app.lionforge.com\/comics\/adminpanel\/episode_image\/015041Quincredible_1-5.png","Episode-description":" avbcabc ","Episode-video":"http:\/\/www.youtube.com\/embed\/5ORBHSJhXew"}]}]
JSONObject jObject=null;
try {
jObject = new JSONObject(response);
JSONArray list = jObject.getJSONArray("Episode Detail");
for (int i = 0; i < list.length(); i++) {
JSONObject element = list.getJSONObject(i);
episodebean bean=new episodebean();
i am getting json exception that JSONArray cannot be converted to JSONObject,how i resolve this ,i am so confuse..,please help me..
use following code
JSONObject jObject=null;
try {
JSONArray array = new JsonArray(response)
jObject = jsonArray.getJSONObject(0);
JSONArray list = jObject.getJSONArray("Episode Detail");
for (int i = 0; i < list.length(); i++) {
JSONObject element = list.getJSONObject(i);
episodebean bean=new episodebean();
your have one jsonArray that has one JsonObject
Change this
jObject = new JSONObject(response);
To
JSONArray jarray = new JSONArray(response);
JSON
[ //json array node
{ // json object node
"Episode Detail": [ // json array episode detail
{ // json obect node j
"Episode-image": "http://app.lionforge.com/comics/adminpanel/episode_image/014929Quincredible_1-6.png",
"Episode-description": " dsdsdsdsds ",
"Episode-video": "http://www.youtube.com/embed/KhXTLAdadlw"
},
{
"Episode-image": "http://app.lionforge.com/comics/adminpanel/episode_image/015041Quincredible_1-5.png",
"Episode-description": " avbcabc ",
"Episode-video": "http://www.youtube.com/embed/5ORBHSJhXew"
}
]
}
]
JSONArray arr = new JSONArray("");
for (int i = 0; i < arr.length(); i++) {
JSONObject c = json_data.getJSONObject(i);
JSONArray arrdata = c.getJSONArray("Episode Detail");
}
The root of your JSON is an JSONArray
response is Array not object.
jObject = new JSONObject(response);
it should be Array instead of object

How to deal with JSON that has no names in android

I'm retrieving the following JSON from a web service:
["test_w","\u062a\u0637\u0628\u064a\u0642 \u0631\u0642\u0645 3","\u062a\u0637\u0628
\u064a \u0642 \u0631\u0642\u0645 2","\u062a\u0637\u0628\u064a\u0642 \u0631\u0642\u0645
1","test_333","\u0645\u0639\u0631\u0641\u0629 \u0627\u0644\u062a\u0627\u0621 \u0627\u0644
\u0645\u0631\u0628\u0648\u0637\u0629 \u0648\u062a\u0646\u0648\u064a\u0646 \u0627\u0644
\u0636\u0645","\u0639\u0644\u0648\u0645","\u0648\u0631\u0642\u0629 \u0639\u0645\u0644"]
As you can see, it's not in name, value format; I tried accessing the JsonObject itself but it's always returning null:
JSONArray jArray = new JSONArray(resultLine);
for (int i = 0; i < jArray.length(); i++) {
JSONObject json_data = jArray.getJSONObject(i);
notifArray.add(json_data.toString());
Is there any way to deal with this particular format of JSON ?
You have an array of strings. So you have to use getString (or optString) to access the array elements:
JSONArray jArray = new JSONArray(resultLine);
for (int i = 0; i < jArray.length(); i++) {
String str = jArray.getString(i);
notifArray.add(str);
}
JSONArray jArray = new JSONArray(resultLine);
for (int i = 0; i < jArray.length(); i++) {
notifArray.add(jArray.get(i));
}

Categories

Resources