Android创建一个Json String

时间:2011-11-10 11:55:31

标签: android json

我正在尝试在Android应用程序中创建一个JSON字符串。

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

这会创建一个像这样的字符串

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

我想创建一个像这样的JSON字符串

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

3 个答案:

答案 0 :(得分:15)

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

这将给你jsonObject,但不是数组,我没有看到使用JSONArray的任何意义。在这种情况下,JSONObject更好。您将看到以下输出为String

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

答案 1 :(得分:6)

Create JSON objects for the jArrayFacebookData (不是你采用的JSONArray)并将 jObjectType jObjectData 放入其中。

检查此JSONObject put object方法。

<强>更新

你的JSON有错误:

enter image description here

有效的JSON是:

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

最终解决方案:

     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)
  {

  }

答案 2 :(得分:0)

我如何发布json字符串。

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