-4

I am beginner in Android. I need to know

1)How to convert object of array to JsonObject. I need in this format

{"House_id":"1"
 "Date":"12.12.2016"
[{"Name":"RickBald","email":"[email protected]","address":"homeaddress1"},{"Name":"EshaRan","email":"[email protected]","address":"homeaddress2"},{"Name":"Hansa Bella","email":"[email protected]","address":"homeaddress3"}...]}

2)what should be the request in volley,If i want to send this `jsonArray.

Thanks in advance

4
  • 1
    can you show your objects in code? Commented Jan 13, 2017 at 7:42
  • Learn How do I ask a good question? and post the code which prepares array of objects. Commented Jan 13, 2017 at 7:46
  • Thanks @ Shashanth. Commented Jan 17, 2017 at 4:51
  • @ TruongHieu i got the proper code from here stackoverflow.com/questions/25013255/…. Thanks Commented Jan 17, 2017 at 4:55

4 Answers 4

3

you first declare arrays :

 String[] name ={"yousuf" , "Mohammed" , "Ali" , "Hamood" , "Alex"};
 String[] emails = {"yousuf@dd" , "Mohammed@dd" , "Ali@dd" , "Hamood@dd" , "Alex@dd"};

any arrays with data ,

then you create JSONArray

 JSONArray array = new JSONArray();

then you make loop to add objects to the array :

for (int i =0; (i < name.length) && (i < emails.length) ; i++ ) {
        JSONObject object = new JSONObject();
        try {
            object.put("name", name[i]);
            object.put("email" , emails[i]);
            array.put(object);
        }catch (JSONException e) {
            e.printStackTrace();
        }
    }

then you can print the results in the console :

 Log.d("Json is " , array.toString());

you will get the result like this :

[{"name":"yousuf","email":"yousuf@dd"},{"name":"Mohammed","email":"Mohammed@dd"},{"name":"Ali","email":"Ali@dd"},{"name":"Hamood","email":"Hamood@dd"},{"name":"Alex","email":"Alex@dd"}]

but you can format it by : Json formatter and validator

and you'll have nice look to the data : enter image description here

here is the whole code:

public void getDataInJsonFormat (){
    String[] name ={"yousuf" , "Mohammed" , "Ali" , "Hamood" , "Alex"};
    String[] emails = {"yousuf@dd" , "Mohammed@dd" , "Ali@dd" , "Hamood@dd" , "Alex@dd"};

    JSONArray array = new JSONArray();

    for (int i =0; (i < name.length) && (i < emails.length) ; i++ ) {
        JSONObject object = new JSONObject();
        try {
            object.put("name", name[i]);
            object.put("email" , emails[i]);
            array.put(object);
        }catch (JSONException e) {
            e.printStackTrace();
        }
    }

    Log.d("Json is " , array.toString());
}
Sign up to request clarification or add additional context in comments.

Comments

1

You could use Gson which has converters from and to JSON representations of your Java models.

Comments

1

You can use GSON lib and Jackson Object mapper

Please see below code for GSON :

Gson gson = new Gson();

T is generic class (DTO class) in below method getJSONString.

String convertedJson = getJSONString(DTO)

public String getJSONString(T dto){
        return gson.toJson(dto);
}

Comments

1

Suppose Your response String or JSON String

JSONObject json = new JSONObject(strResponse);
Json.putString("Name","RickBald")

put all the value in jsonObject like Json.putString("Name","RickBald");

And it convert into jsonArray

 JSONArray jsonarr = new JSONArray();
 jsonarr.put(json);

OutPut:

[{"Name":"RickBald","email":"[email protected]","address":"homeaddress1"},{"Name":"EshaRan","email":"[email protected]","address":"homeaddress2"},{"Name":"Hansa Bella","email":"[email protected]","address":"homeaddress3"}...]

1 Comment

json.put("Name","RickBald") not Json.putString("Name","RickBald")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.