How to convert the json array to string array in android? - android

I am converting an json array to string array thats doesn't exist any tag. I tried with all tricks but not succeeded.
json array:-
"validValues":["01_Abacus","02_AlarmClock","03_Basketball","04_Beaker","55_Watch"]
Code for convert the json array to string values
if (jsonComponentObj.has(TAG_VALID_VALUES)) {
String value = jsonComponentObj.getString(TAG_VALID_VALUES);
Logs.e("value " + value);
if (!value.equals("null")) {
JSONArray jsonArray = jsonComponentObj
.getJSONArray(TAG_VALID_VALUES);
if (jsonArray != null) {
ArrayList<String> stringArray = new ArrayList<String>();
for (int j = 0; j < jsonArray.length(); j++) {
try {
JSONObject jsonObject = jsonArray
.getJSONObject(j);
stringArray.add(jsonObject.toString());
} catch (JSONException e) {
Logs.e("Exception: "+e.toString());
e.printStackTrace();
}
}
}
}
}
Exception:
org.json.JSONException: Value 01_Abacus at 0 of type java.lang.String cannot be converted to JSONObject
If anyone have idea. Please reply. Thanks in advance..

Because validValues JSONArray contain only Strings instead of JSONObject.so get all values from JSONArray as:
for (int j = 0; j < jsonArray.length(); j++) {
String str_value = jsonArray.optString(j);
stringArray.add(str_value);
}

Your json array contains Strings not json object.Therefore to get Strings from json array directly use getString(),So
Change
JSONObject jsonObject = jsonArray.getJSONObject(j);
stringArray.add(jsonObject.toString());
to
stringArray.add(jsonArray.getString(j));
or
stringArray.add(jsonArray.optString(j));

You should use optString(int) to coerce a string value from array. Using getString(int) would cause problems if ever the values in the array were not strings.
for (int j = 0; j < jsonArray.length(); j++) {
String value = jsonArray.optString(j);
stringArray.add(value);
}
If you want to give a default value, use optString(int, String).

try JSONObject jsonObject = jsonArray.getString(j);
instead of JSONObject jsonObject = jsonArray.getJSONObject(j);

Related

Convert JSONArray string value into int value

I need to convert the value from key "Quantity" into a int.
Basically I have this:
[{"Code":"NV","Quantity":"333"},{"Code":"NV","Quantity":"333"}]
Need to convert to this:
[{"Code":"NV","Quantity":333},{"Codigo":"NV","Quantity":333}]
How can I do it?
Assuming your json data in string and setting it in data string
String data = "[{\"Code\":\"NV\",\"Quantity\":\"333\"},{\"Code\":\"NV\",\"Quantity\":\"333\"}]";
try {
JSONArray jsonArray = new JSONArray(data);
Log.d(TAG, "Old JSONArray: " + jsonArray); // [{"Code":"NV","Quantity":"333"},{"Code":"NV","Quantity":"333"}]
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
int quantityValue = Integer.parseInt(jsonObject.getString("Quantity"));
jsonObject.put("Quantity", quantityValue);
}
Log.d(TAG, "New JSONArray: " + jsonArray); // [{"Code":"NV","Quantity":333},{"Code":"NV","Quantity":333}]
} catch (JSONException e) {
e.printStackTrace();
}
What I am doing here is just replacing old Quantity string value with int value by using Integer.parseInt()
Please try the following:
for(int i = 0; i < array.length(); i++){
JSONObject object = array.getJSONObject(i);
object.put("Quantity", Integer.parseInt(object.getString("Quantity")));
}
You will need to replace array with the name of your array.

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);
}

JAVA parsing inner JSON

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
}

How to add values to a string array in android

I have a problem with my code,
I have a json array
[{"Response":{"data":"sibin1"}},{"Response":{"data":"sibin2"}},
{"Response": {"data":"sibin3"}}]
And iam trying to extract the json data using the below code,Here i added only some parts of the coode
JSONArray finalResult = new JSONArray(tokener);
int finalresultlengt=finalResult.length();
JSONObject json_data = new JSONObject();
for (int i = 0; i < finalResult.length(); i++)
{
json_data = finalResult.getJSONObject(i);
System.out.println("json dataa"+json_data.names().toString());
JSONObject menuObject = json_data.getJSONObject("Response");
result= menuObject.getString("data");
System.out.println(result);
}
The code is worked very well
when the value of
i=0 ,result is sibin1
i=1 ,result is sibin2
i=2 ,result is sibin3
But my problem is , i need to store the result in a string array of length finalresultlength inside the given for loop, also i need to print the values in the string array in a for loop outside the given for loop
if anybody knows please help me...........
You could do this way as well.
Create an ArrayList of size 'finalresultlengt' and the add the values in.
list.add(result); // variable 'result' in your case is the value from JSON
If you have more values to be added, create a POJO class.
class POJO {
private String dataVal;
public void setDataVal(String dataVal) {
this.dataVal = dataVal;
}
public String getDataVal() {
return dataVal;
}
}
Then create an ArrayList of type POJO.
ArrayList<POJO> list = new ArrayList<POJO>(finalresultlengt);
EDIT
JSONArray finalResult = new JSONArray(tokener);
int finalresultlengt=finalResult.length();
JSONObject json_data = new JSONObject();
ArrayList<String> list = new ArrayList<String>(finalresultlengt);
for (int i = 0; i < finalResult.length(); i++) {
json_data = finalResult.getJSONObject(i);
System.out.println("json dataa"+json_data.names().toString());
JSONObject menuObject = json_data.getJSONObject("Response");
result= menuObject.getString("data");
list.add(result);
}
Populate values from ArrayList.
for(String value : list)
System.out.println(value);

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