how can parsing this output
{
"durum": "tamam",
"mahalleler": [
{
"mahalle_kodu": "1",
"mahalle_ismi": "BEKTAŞ MAH."
},
{
"mahalle_kodu": "2",
"mahalle_ismi": "ÇARŞI MAH."
}]}
i try this code but return "null".
contacts = json.getJSONArray("mahalleler");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String name = c.getString("mahalle_kodu");
String body = c.getString("mahalle_ismi");}
what is the problem? json output has two variable; mahalleler[] and durum. i wanna parse durum's valur and values of mahalleler array. But i couldnt do that.
JSONObject json = new JSONObject("{
"durum": "tamam",
"mahalleler": [
{
"mahalle_kodu": "1",
"mahalle_ismi": "BEKTAŞ MAH."
},
{
"mahalle_kodu": "2",
"mahalle_ismi": "ÇARŞI MAH."
}]}
");
String name[]
String body[]
JSONArray contacts = json.getJSONArray("mahalleler");
name=new String[contacts.length()];
body =new String[contacts.length()];
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
name[i] = c.getString("mahalle_kodu");
body[i] = c.getString("mahalle_ismi");}
i think u r nt geting o/p as u declared the strings inside the for loop
You hve to do get json array like this
JSONArray mahallelerArray = c.getJSONArray("mahalleler");
you need to check if the content is in the json:
if (json != null && !json.isNull("mahalleler")) {
JSONArray contacts = json.getJSONArray("mahalleler");
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String name = c.getString("mahalle_kodu");
String body = c.getString("mahalle_ismi");}
}
You are declaring your String inside your for loop. Make them class variables arrays to store the data.
Related
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+"");
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");
}
I am trying to take "key" value in a JSON file. I tried many solutions but I couldn't handle this ,it constantly gives me "at videos of type org.json.JSONObject cannot be converted to JSONArray" error. How can I solve this. Thank you.
My JSON data;
{ videos: {
results:[
{
id: "56c4ccbfc3a3680d52000610",
iso_639_1: "en",
iso_3166_1: "US",
key: "nIGtF3J5kn8",
}
]
}
}
My code;
JSONArray Movie_List = Search_Results.getJSONArray("videos");
for (int i = 0; i < Movie_List.length(); i++)
{
JSONObject movie = (JSONObject) Movie_List.get(i);
JSONArray Movie_List1 = movie.getJSONArray("results");
for (int j = 0; j < Movie_List1.length(); j++)
{
JSONObject movie2 = (JSONObject) Movie_List1.get(j);
key = movie2.getString("key");
}
}
The error;
Try some like this.. Your mistake come because you try to parse videos to JSONArray but it is JSONObject...
JSONObject videos = Search_Results.getJSONObject("videos");
JSONArray result= videos.getJSONArray("results");
for (int i = 0; i < result.length(); i++)
{
JSONObject movie = result.getJSONObject(i);
String movieId = movie.getString("id");
//and other values .. same way
}
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...
I have this data form this URL
http://api.clicky.com/api/stats/4?site_id=32020&sitekey=71bf7c1681e22468&type=visitors&output=json&json_callback=data%22
and how to get this in android,
my problem is how to access all this objects and array, help me
[
{
"type": "visitors",
"dates": [
{
"date": "2014-12-25",
"items": [
{ "value":"70" }
]
}
]
}
]
JSONArray jsonArray = new JSONArray("here is your json string ") ;
int count = jsonArray.length() ;
for (int i = 0; i < count; i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i) ;
String type = jsonObject.optString("type") ;
JSONArray datesArray = jsonObject.optJSONArray("dates") ;
int datesCount = datesArray.length() ;
for(int j = 0; j< datesCount; j++){
JSONObject dateItem = datesArray.get(j) ;
String dateStr = dateItem.optString("date");
JSONArray itemsArray = dateItem.optJSONArray("items");
for(int f = 0 ; f < itemsArray.length(); f++){
JSONObject valueJson = itemsArray.get(f);
String value = valueJson.optString("value");
}
}
}
I think, you can use GSON library for parsing your JSON. Just create class for JSON and use this code:
new Gson().fromJson(json, MyGsonClass.class);