Extracting values from json file android - android

My json string is as follows:
i pass it to a json object as follows but i get only the golfs value.
jsonObject = new JSONObject(details_circuit);
System.out.println("jsonObject "+jsonObject.toString());
jsArray = jsonObject.getJSONArray("golfs");
if i do
jsonObject1 = new JSONObject(jsonObject.getString("circuit"));
i cannot get any value. Any idea.

jsonObject1 = new JSONObject(jsonObject.getString("circuit"));//Wrong because circuit is a jsonObject
Use this instead
JSONObject jsonObject1 = jsonObject.getJSONObject("circuit");
or use
JSONObject circuit = (JSONObject)jsonObject.get('circuit');
Reference

JSONObject jsonObject = new JSONObject(details_circuit);
JSONObject circuit=jsonObject.getJSONObject("circuit");
int id=circuit.getInt("#id");
int version=circuit.getInt("#version");
String name=circuit.getString("#name");
String description=circuit.getString("#description");
JSONObject row;
int id ;
int version;
String name;
JSONArray golfs=jsonObject.getJSONObject("golfs");
for(int i=0;i<golfs.length();i++){
row = golfs.getJSONObject(i);
id = row.getInt("id");
version=row.getInt("#version");
name=row.getString("#name");
}

"circuit": { is a JSONObject
Change to
jsonObject1 = (JSONObject)jsonObject.getJSONObject("circuit");
int id = jsonObject1.getInt("#id"):

jsonObject1 = jsonObject.getJSONObject("circuit");

Use following code.
JSONObject json = jParser.getJSONFromUrl(url);
JSONObject json1 = json.getJSONObject("circuit");
int id = json1.getInt("id");
For JsonArray Use following.
JSONArray jarray = json.getJSONArray("golfs");
JSONObject jarray = json.getJSONObject(0);
int id = json1.getInt("id");

Related

Android Studio: of type org.json.JSONObject cannot be converted to JSONArray

I get error: of type org.json.JSONObject cannot be converted to JSONArray when I try to retrieve JSON values.
JSONArray jsonarray = new JSONArray(data);
for(int i = 0; i < jsonarray.length(); i++){
JSONObject JO = (JSONObject) jsonarray.get(i);
String id = JO.getString("languages");
dataParsed = dataParsed + id;
}
The JSON looks like this (have removed sensitive data)
{
"ip":"",
"type":"",
"continent_code":"EU",
"continent_name":"Europe",
"country_code":"GB",
"country_name":"United Kingdom",
"region_code":"ENG",
"region_name":"England",
"city":"",
"zip":"",
"latitude":,
"longitude":,
"location":{
"geoname_id":,
"capital":"London",
"languages":[
{
"code":"en",
"name":"English",
"native":"English"
}
],
"country_flag":"http:\/\/assets.ipstack.com\/flags\/gb.svg",
"country_flag_emoji":"\ud83c\uddec\ud83c\udde7",
"country_flag_emoji_unicode":"U+1F1EC U+1F1E7",
"calling_code":"44",
"is_eu":true
}
}
I should mention that the data I actually want to retrieve is the name of the language.
You could try this:
JSONObject obj = new JSONObject(data);
JSONObject objLocation = obj.getJSONObject("location");
JSONArray arrayLanguage = objLocation.getJSONArray("languages");
for (int i = 0; i < arrayLanguage.length(); i++) {
JSONObject JO = (JSONObject) arrayLanguage.get(i);
String id = JO.getString("languages");
dataParsed = dataParsed + id;
}

how to get json which has multiple values in a string in android

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+"");

how to convert JSONObject to JSONArray

JSONObject user_values = new JSONObject(json.getString("quotes_cat"));
JSONArray user_subject = new JSONArray(
json.getString("quotes_cat"));
List<String> list = new ArrayList<String>();
for (int i = 0; i < user_subject.length(); i++)
{
list.add(user_subject.getString(i));
}
String[] stringArray = list.toArray(new String[list.size()]);
The problem is
type org.json.JSONArray cannot be converted to JSONObject
JSON output is this
{"status":"ok","response":"Fetched Successfully!","quotes_cat":["Life","Love","Success","Happiness","Funny","Love","Success","Happiness","Funny"]}
Replace this
JSONObject user_values = new JSONObject(
json.getString("quotes_cat"));
with this:
JSONObject user_values = new JSONObject(json);
You need to parse the original JSON String into an JSONObject Java object this way. After that, replace this:
JSONArray user_subject = new JSONArray(
json.getString("quotes_cat"));
with this:
JSONArray user_subject = user_values.getJSONArray("quotes_cat"));
to get the array out of the newly created root JSONObject :-)
Your quotes_cat is jsonArray, so try this:
JSONArray user_subject = new JSONObject(json).getJSONArray("quotes_cat");
List<String> list = new ArrayList<String>();
for (int i = 0; i < user_subject.length(); i++)
{
list.add(user_subject.getString(i));
}
String[] stringArray = list.toArray(new String[list.size()]);
When json is your json string:
String json = "{\"status\":\"ok\",\"response\":\"Fetched Successfully!\",\"quotes_cat\":[\"Life\",\"Love\",\"Success\",\"Happiness\",\"Funny\",\"Love\",\"Success\",\"Happiness\",\"Funny\"]}";
Assuming that json class is JSONObject, I suggest you to use getJSONArray("quotescat") to retrieve your array.

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 get maximum/largest value from json object with Android?

I've json string like this:
{"GetReportResult":[
{"bulan":"4","total":"2448","type":"CHEESE1K","uang":"8847823"},{"bulan":"4","total":"572476","type":"ESL","uang":"5863408410"},{"bulan":"4","total":"46008","type":"ESL500ML","uang":"234498301"},{"bulan":"4","total":"228985","type":"UHT","uang":"1367172990"},{"bulan":"4","total":"40995","type":"WHP","uang":"235750452"},{"bulan":"5","total":"5703","type":"CHEESE1K","uang":"134929306"},{"bulan":"5","total":"648663","type":"ESL","uang":"6645764498"},{"bulan":"5","total":"49305","type":"ESL500ML","uang":"266817346"},{"bulan":"5","total":"287867","type":"UHT","uang":"1446897805"},{"bulan":"5","total":"51958","type":"WHP","uang":"631994613"},{"bulan":"6","total":"4390","type":"CHEESE1K","uang":"104527773"},{"bulan":"6","total":"443335","type":"ESL","uang":"4540123082"},{"bulan":"6","total":"28462","type":"ESL500ML","uang":"148290912"},{"bulan":"6","total":"213250","type":"UHT","uang":"1197646870"},{"bulan":"6","total":"27049","type":"WHP","uang":"189802525"}
]}
I want to get maximum/largest value of bulan, which is 6. How to do that on android?
This should do it.
JSONObject json = new JSONObject("your json string");
JSONArray jsonArray = json.getJSONArray("GetReportResult");
int max = -1;
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject temp = jsonArray.getJSONObject(i);
if(temp.getInt("bulan") > max)
max = temp.getInt("bulan");
}
Log.e("Max", "" + max);

Categories

Resources