7

I am trying to create a JSON String in the Android application.

    JSONArray jArrayFacebookData = new JSONArray();
    JSONObject jObjectType = new JSONObject();

    // put elements into the object as a key-value pair
    jObjectType.put("type", "facebook_login");

    jArrayFacebookData.put(jObjectType);

    // 2nd array for user information
    JSONObject jObjectData = new JSONObject();


    // Create Json Object using Facebook Data
    jObjectData.put("facebook_user_id", id);
    jObjectData.put("first_name", first_name);
    jObjectData.put("last_name", last_name);
    jObjectData.put("email", email);
    jObjectData.put("username", username);
    jObjectData.put("birthday", birthday);
    jObjectData.put("gender", gender);
    jObjectData.put("location", place);
    jObjectData.put("display_photo", display_photo_url);

    jArrayFacebookData.put(jObjectData);

Which creates a string like this

[
   {
      "type":"facebook_login"
   },
   {
      "birthday":"06\/22\/1986",
      "first_name":"Harsha",
      "username":"harshamv",
      "location":"Bangalore, India",
      "email":"[email protected]",
      "last_name":"Mv",
      "gender":"male",
      "facebook_user_id":"1423671254",
      "display_photo":"http:\/\/graph.facebook.com\/1423671254\/picture?type=large"
   }
]

I want to create a JSON string something like this

[
   "system":{
      "type":"facebook_login"
   },
   "data":{
      "birthday":"06\/22\/1986",
      "first_name":"Harsha",
      "username":"harshamv",
      "location":"Bangalore, India",
      "email":"[email protected]",
      "last_name":"Mv",
      "gender":"male",
      "facebook_user_id":"1423671254",
      "display_photo":"http:\/\/graph.facebook.com\/1423671254\/picture?type=large"
   }
]

3 Answers 3

20
JSONObject jArrayFacebookData = new JSONObject();
    JSONObject jObjectType = new JSONObject();

    // put elements into the object as a key-value pair
    jObjectType.put("type", "facebook_login");

    jArrayFacebookData.put("system", jObjectType);

    // 2nd array for user information
    JSONObject jObjectData = new JSONObject();


    // Create Json Object using Facebook Data
    jObjectData.put("facebook_user_id", id);
    jObjectData.put("first_name", first_name);
    jObjectData.put("last_name", last_name);
    jObjectData.put("email", email);
    jObjectData.put("username", username);
    jObjectData.put("birthday", birthday);
    jObjectData.put("gender", gender);
    jObjectData.put("location", place);
    jObjectData.put("display_photo", display_photo_url);

    jArrayFacebookData.put("data", jObjectData);

this will give you jsonObject, but not array, i don't see any point in using JSONArray. JSONObject is better in this case. you will see following output as String

{
   "system":{
      "type":"facebook_login"
   },
   "data":{
      "birthday":"06\/22\/1986",
      "first_name":"Harsha",
      "username":"harshamv",
      "location":"Bangalore, India",
      "email":"[email protected]",
      "last_name":"Mv",
      "gender":"male",
      "facebook_user_id":"1423671254",
      "display_photo":"http:\/\/graph.facebook.com\/1423671254\/picture?type=large"
   }
}
Sign up to request clarification or add additional context in comments.

3 Comments

do you mean to say you can't put jsonObject inside a json Object?
Sorry my dear for my mistake. I haven't seen that you have taken jArrayFacebookData is of JSONObject, not of JsonArray.
that is because, jsonArray cannot have key value as it's elements , that is why i suggested to use jsonObject
7

Create JSON objects for the jArrayFacebookData (not JSONArray as you have taken) and put jObjectType and jObjectData inside it.

Check this JSONObject put object method.

Update:

Your JSON is having error:

enter image description here

Valid JSON is:

{
    "system": {
        "type": "facebook_login"
    },
    "data": {
        "birthday": "06/22/1986",
        "first_name": "Harsha",
        "username": "harshamv",
        "location": "Bangalore, India",
        "email": "[email protected]",
        "last_name": "Mv",
        "gender": "male",
        "facebook_user_id": "1423671254",
        "display_photo": "http://graph.facebook.com/1423671254/picture?type=large"
    }
}

Final Solution:

     try
        {
    JSONObject jArrayFacebookData = new JSONObject();

        JSONObject jObjectType = new JSONObject();
        jObjectType.put("type", "facebook_login");

        JSONObject jObjectData = new JSONObject();
        jObjectData.put("facebook_user_id", "2323");
        jObjectData.put("first_name", "2323");
        jObjectData.put("last_name", "2323");
        //put other data here   

    jArrayFacebookData.put("system", jObjectType);
    jArrayFacebookData.put("data",jObjectData);

    System.out.println("==========> Final output => "+jArrayFacebookData.toString());

  }
  catch(Exception e)
  {

  }

Comments

0

how i post json string.

for(int i=0; i<iArr.size(); i++){
    if(i==0){
        st = "{\"userId\":" + iArr.get(i) + "}";
        str += st;
    }else if(i>0 && i<iArr.size()-1){
        st = ",{\"userId\":" + iArr.get(i) + "}";
        str+=st;
    }else if(i==iArr.size()){
        st = ",{\"userId\":" + iArr.get(i) + "}]}";
        str+=st;
    }
}
String myPost = "{\"project\":{\"Name\":"+ "\""+ title + "\""
              + ",\"Description\":" + "\""+ desc + "\""
              + ",\"createdBy\":" + usrid + ""
              + ",\"startDate\":" + "\""+ startdate + "\""
              + ",\"dueDate\":" + "\""+ duedate + "\""
              + ",\"projectLeadId\":" + leadPosition + ""
              + ",\"QAId\":" + QAssurencePosition + ""
              + ",\"TotalHour\":" +"\""+ edtHour + "\""+ "},\"members\":[";
                myPost += str;
                myPost +="]}";
                RequestPackage myPackage = new RequestPackage();
                myPackage.setUri(getaddProject);
                myPackage.setMethod("POST");
                myPackage.setParam("My Post",myPost+"");
                new MyTask().execute(myPackage);

                Toast.makeText(CreateProject.this,"Testing String: " + myPost,Toast.LENGTH_LONG ).show();

                Log.d("My Post :",myPost);
 }

Comments

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.