我正在尝试在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);
Run Code Online (Sandbox Code Playgroud)
这会创建一个像这样的字符串
[
{
"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"
}
]
Run Code Online (Sandbox Code Playgroud)
我想创建一个像这样的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"
}
]
Run Code Online (Sandbox Code Playgroud)
Yas*_*mar 16
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);
Run Code Online (Sandbox Code Playgroud)
这将给你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"
}
}
Run Code Online (Sandbox Code Playgroud)
Create JSON objects for the jArrayFacebookData(不是你已经采用的JSONArray)并将jObjectType和jObjectData放在其中.
检查这个JSONObject put对象方法.
更新:
您的JSON有错误:

有效的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"
}
}
Run Code Online (Sandbox Code Playgroud)
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)
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
18289 次 |
| 最近记录: |