I am putting a list in the json and now I want to get the list and display each entry in different textview. This is my code to put the list in json and to get the same
static ArrayList<String> list = new ArrayList<String>();
JSONObject json = jsonFruitArray.getJSONObject(i);
list.add("john");
list.add("mat");
list.add("jason");
json.put("sku",new JSONArray(list));
This is how i am trying to get the list
JSONArray jsonFruitSkuArray = json.getJSONArray("sku");
for(int iSku=0; iSku< jsonFruitSkuArray.length();iSku++){
JSONObject jsonSkuObject = jsonFruitSkuArray.getJSONObject(iSku);
productBean.setSkuId1(jsonSkuObject.getInt("sku"));
}
You should try like this. Use JsonArray
JSONObject jsonObject = new JSONObject();
try {
jsonObject.put("1", 1);
jsonObject.put("2", 2);
jsonObject.put("3", 3);
} catch (JSONException e) {
e.printStackTrace();
}
JSONArray result = new JSONArray();
result.put(jsonObject);
//Use json array
JSONObject jsonObj = new JSONObject();
JSONArray jsonArray = new JSONArray();
jsonArray.put("John");
jsonArray.put("Mat");
jsonArray.put("Jason");
jsonObj.put("json_array", jsonArray);
//Retrieve like this
JSONArray jsonFruitSkuArray = jsonObj.getJSONArray("json_array");
for(int iSku=0; iSku< jsonFruitSkuArray.length();iSku++){
JSONObject jsonSkuObject = jsonFruitSkuArray.getJSONObject(iSku);
productBean.setSkuId1(jsonSkuObject.getInt("sku"));
}
In your code you can try following :
ArrayList<String> list = new ArrayList<String>();
JSONObject json = new JSONObject();
list.add("john");
list.add("mat");
list.add("jason");
json.put("sku", new JSONArray(list));
JSONArray jsonFruitSkuArray = json.getJSONArray("sku");
for (int iSku = 0; iSku < jsonFruitSkuArray.length(); iSku++) {
String element = jsonFruitSkuArray.getString(iSku);
System.out.println(element);
}
Related
For post method in android
I have to send array of objects.
I am retrieving elements from an arrayList and putting that retrieved element in JSONObject and than putting that in JSONobject in JSONArray.
JSONArray ccArray = new JSONArray();
{
JSONObject object = new JSONObject();
if (ccArrayList.size() != 0) {
for (int i = 0; i < ccArrayList.size(); i++) {
object.put("emailId", ccArrayList.get(i));
ccArray.put(object);
}
} else {
object.put("", "");
}
}
When there are more than 2 or more than 2 elements in arraylist it is adding the last element in ccrray as many times as their are elements in the list.
output :
"cc":[{"emailId":"f#j.com"},{"emailId":"f#j.com"}]
Change code like this
JSONObject obj = new JSONObject();
JSONArray ccArray = new JSONArray();
for (int i = 0; i < ccArrayList.size(); i++) {
JSONObject object = new JSONObject();
if (ccArrayList.size() != 0) {
object.put("emailId", ccArrayList.get(i));
ccArray.put(object);
} else {
object.put("", "");
}
}
obj.put("cc",ccArray);
Code:-
ArrayList<String> ccArrayList = new ArrayList<String>();
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
ccArrayList.add("abc#xyz.com");
JSONArray ccArray = new JSONArray();
if(ccArrayList.size()>0){
for(int i=0;i<ccArrayList.size();i++){
JSONObject object = new JSONObject();
try {
object.put("emailId", ccArrayList.get(i));
} catch (JSONException e) {
e.printStackTrace();
}
ccArray.put(object);
}
}
Output:-
ccArray: [{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"},{"emailId":"abc#xyz.com"}]
That's because you declared JSONObject object = new JSONObject(); outside the for loop, so each time it loops the object adds a new JSONObject and also does the array. Try keeping it within the loop:
JSONArray ccArray = new JSONArray();
for (int i = 0; i < ccArrayList.size(); i++) {
JSONObject object = new JSONObject();
object.put("emailId", ccArrayList.get(i));
ccArray.put(object);
}
How can I create a in android json like this.I can create the json object but still facing some problem to make like this.
{
"IncidentDetails": {
"StreetId": "370","StartDate":"21-03-2015","EndDate":"24-03-2015",
"ImageDetails": [
{
"PhoneImageUrl": "\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150321_215458_-926186234.jpg",
"Base64ImageData": "Ioop9RLYCKTAIzRRSGIVGw9s1hvw5HvRRVRYtixan+daM2pCQ\/PH40nQ0uOOKM880guIQTmm044xnNNx",
},
{
"PhoneImageUrl": "\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150321_215458_-926186234.jpg",
"Base64ImageData": "Ioop9RLYCKTAIzRRSGIVGw9s1hvw5HvRRVRYtixan+daM2pCQ\/PH40nQ0uOOKM880guIQTmm044xnNNx",
},
{
"PhoneImageUrl": "\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150321_215458_-926186234.jpg",
"Base64ImageData": "Ioop9RLYCKTAIzRRSGIVGw9s1hvw5HvRRVRYtixan+daM2pCQ\/PH40nQ0uOOKM880guIQTmm044xnNNx",
}
]}
}
What I tried in my android code just have a look it giving totally different output
JSONArray jsonArray = new JSONArray();
JSONObject IncidentDetails = new JSONObject();
try
{
IncidentDetails.put("StartDate", EditTextStartText.getText().toString());
IncidentDetails.put("EndDate", EditTextEndText.getText().toString());
IncidentDetails.put("StreetId", StreetId);
jsonArray.put(IncidentDetails);
for(int i=0; i<length; i++)
{
JSONObject ImageDetails = new JSONObject();
ImageDetails.put("Base64ImageData", getBase64Image(ReusableClass.imgUrl.get(i)));
ImageDetails.put("PhoneImageUrl", ReusableClass.imgUrl.get(i));
jsonArray.put(ImageDetails);
}
JSONObject IncidentDetailsObj = new JSONObject();
IncidentDetailsObj.put("IncidentDetails", jsonArray);
String jsonStr = IncidentDetailsObj.toString();
My current output
{"IncidentDetails":[{"StreetId":"370","StartDate":"18-03-2015","EndDate":""},{"PhoneImageUrl":"\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150318_171451_1389057690.jpg","Base64ImageData":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkz\nODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P\/2wBDARESEhgVGC8aGi9jQjhCY2NjY2Nj\nY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P\/wAA"},{"PhoneImageUrl":"\/storage\/sdcard0\/Pictures\/GestiónDeIncidencias\/IMG_20150318_171451_1389057690.jpg","Base64ImageData":"\/9j\/4AAQSkZJRgABAQAAAQABAAD\/2wBDABALDA4MChAODQ4SERATGCgaGBYWGDEjJR0oOjM9PDkz\nODdASFxOQERXRTc4UG1RV19iZ2hnPk1xeXBkeFxlZ2P\/2wBDARESEhgVGC8aGi9jQjhCY2NjY2Nj\nY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2NjY2P\/wAA"}]}
Use this
JSONArray jsonArray = new JSONArray();
JSONObject IncidentDetails = new JSONObject();
try
{
IncidentDetails.put("StartDate", EditTextStartText.getText().toString());
IncidentDetails.put("EndDate", EditTextEndText.getText().toString());
IncidentDetails.put("StreetId", StreetId);
for(int i=0; i<length; i++)
{
JSONObject ImageDetails = new JSONObject();
ImageDetails.put("Base64ImageData", getBase64Image(ReusableClass.imgUrl.get(i)));
ImageDetails.put("PhoneImageUrl", ReusableClass.imgUrl.get(i));
jsonArray.put(ImageDetails);
}
IncidentDetails.put("ImageDetails", jsonArray);
JSONObject IncidentDetailsObj = new JSONObject();
IncidentDetailsObj.put("IncidentDetails", IncidentDetails);
String jsonStr = IncidentDetailsObj.toString();
see answer with comments,
//* IncidentalDetial is an object not an array
JSONObject IncidentDetails = new JSONObject();
//* add the first three elements
IncidentDetails.put("StartDate", EditTextStartText.getText().toString());
IncidentDetails.put("EndDate", EditTextEndText.getText().toString());
IncidentDetails.put("StreetId", StreetId);
//* contruct the imagedetails array
JSONArray jsonArray = new JSONArray();
for(int i=0; i<length; i++)
{
JSONObject ImageDetails = new JSONObject();
ImageDetails.put("Base64ImageData", getBase64Image(ReusableClass.imgUrl.get(i)));
ImageDetails.put("PhoneImageUrl", ReusableClass.imgUrl.get(i));
jsonArray.put(ImageDetails);
}
//* add the imagedetails array to the incidentDetials object
IncidentDetails.put(jsonArray);
JSONObject IncidentDetailsObj = new JSONObject();
IncidentDetailsObj .put(IncidentDetails);
String jsonStr = IncidentDetailsObj.toString();
Notice that that this is very common scenario, so there has to be some library that deals it for you :)
I recommend https://code.google.com/p/google-gson - GSON. All you have to do is to prepare POJO class that describes JSON, in your case:
public class IncidentDetails {
IncidentDetails incidentDetails;
}
public class IncidentDetails {
String streetId;
String startDate;
String endDate;
ImageDetails imageDetails;
}
Then do the same with ImageDetails. All you have to do then is to create GSON with proper naming policy (it converts automaticcaly class fields names to JSON).
gson.toJson(incidentDetails) creates String you desire. gson.fromString(String json) will create IncidentDetails object from json string.
I'm trying to parse the json format:
{"data":["Laptop","Desktop","Ultrabook"]}
but this seems to give me an error:
JSONArray jr = new JSONArray(jsonStr);
I tried to parse the strings instead, but i doubt thats the best practice.
And, I couldn't figure out how to parse the quotations out of the string.
Change this
JSONArray jr = new JSONArray(jsonStr);
to
JSONObject jsonobject = new JSONObject(jsonStr);
Then
JSONArray jr = jsonobject.getJSOnArray("data");
for(int i=0;i<jr.length();i++)
{
String value =(String) jr.get(i);
}
Your json
{ // json object node
"data": [ // json array data
"Laptop",
"Desktop",
"Ultrabook"
]
}
That's because you are not getting a JSONArray but JSONObject (the root element is not an array). Try this:
JSONObject data = new JSONObject(jsonStr).getJSONArray("data");
do :
JSONObject obj = new JSONObject(jsonStr);
JSONArray arr = obj.getJSONArray("data");
your first tag in JSONObject not a JSONArray.
So you need to change this from
JSONArray jr = new JSONArray(jsonStr);
to
JSONObject jObject = new JSONObject(jsonStr);
try like this
String data = "{\"data\":[\"Laptop\",\"Desktop\",\"Ultrabook\"]}";
try {
JSONObject jObj = new JSONObject(data);
JSONArray jsonArray = new JSONArray();
jsonArray= jObj.optJSONArray("data");
for (int i = 0; i < jsonArray.length(); i++) {
System.out.println(jsonArray.get(i).toString());
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
l get string with answer from server. l want to do it JSONObject.
l do
JSONObject jsonObj = new JSONObject(json);
in json has
"{"sentences":[{"trans":"R\u0455R\u0491ReR\u0405","orig":"�\u0455�\u0491��\u0405","translit":"","src_translit":"R\u1E91Rg\u0300RoR\u1E90"}],"src":"ru","server_time":1}"
but jsonObj has
"{"sentences":[{"src_translit":"RẑRg̀RoRẐ","orig":"�ѕ�ґ��Ѕ","trans":"RѕRґReRЅ","translit":""}],"server_time":1,"src":"ru"}"
so how l can get value from "trans":"RѕRґReRЅ"?
PS RѕRґReRЅ it's "own" in normal charset
If you already have the jsonObj you just have to do:
try {
// Getting Array of Sentences
sentences = json.getJSONArray("sentences");
// looping through All Contacts
for(int i = 0; i < sentences.length(); i++){
JSONObject c = sentences.getJSONObject(i);
// get the value from trans
String trans = c.getString("trans");
//now you should save this string in an array
}
} catch (JSONException e) {
e.printStackTrace();
}
try this
JSONObject jsonObj = new JSONObject(json);
JSONArray sentences = jsonObj.getJSONArray("sentences");
for(int i=0;i<sentences.length();i++){
JSONObject number = sentences.getJSONObject(i);
String transValue = number.getString("trans");
}
First you have to remove single quote form starting and from ending of your JSON string (json), by this your JSON will be a valid json for this you have to do like :
json= json.substring(1, json.length()-1);
After that you do like this :
JSONObject oJsonObject = new JSONObject(json);
JSONArray oJsonArray = oJsonObject.getJSONArray("sentences");
for(int i=0; i<oJsonArray.length(); i++)
{
JSONObject oJsonObject1 = oJsonArray.getJSONObject(i);
String transValue = oJsonObject1 .getString("trans");
}
I am trying to get a list of available numbers from the following json object, using the class from org.json
{
"response":true,
"state":1,
"data":
{
"CALLERID":"81101099",
"numbers":
[
"21344111","21772917",
"28511113","29274472",
"29843999","29845591",
"30870001","30870089",
"30870090","30870091"
]
}
}
My first steps were, after receiving the json object from the web service:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
Now, how do I save the string array of numbers?
use:
jsonObj = new JSONObject(response);
jsonData = jsonObj.optJSONObject("data");
JSONArray arrJson = jsonData.getJSONArray("numbers");
String[] arr = new String[arrJson.length()];
for(int i = 0; i < arrJson.length(); i++)
arr[i] = arrJson.getString(i);
you need to use JSONArray to pull data in an array
JSONObject jObj= new JSONObject(your_json_response);
JSONArray array = jObj.getJSONArray("data");
Assuming that you are trying to get it in a javascript block, Try something like this
var arrNumber = jsonData.numbers;
My code is for getting "data":
public void jsonParserArray(String json) {
String [] resultsNumbers = new String[100];
try {
JSONObject jsonObjectGetData = new JSONObject(json);
JSONObject jsonObjectGetNumbers = jsonObjectGetData.optJSONObject("results");
JSONArray jsonArray = jsonObjectGetNumbers.getJSONArray("numbers");
for (int i = 0; i < jsonArray.length(); i++) {
resultsNumbers[i] = jsonArray.getString(i);
}
} catch (JSONException e) {
e.printStackTrace();
Log.e(LOG_TAG, e.toString());
}
}