2013-10-18 84 views
0

要求的格式如何改變JSON對象

{ 
    "credentials": { 
     "username": "foobar2", 
     "password": "Demandware1" 
    }, 
    "profile": { 
     "email": "[email protected]", 
     "birthday": "2011-05-06", 
     "fax": "", 
     "first_name": "", 
     "gender": "m", 
     "job_title": "", 
     "last_name": "", 
     "phone_business": "", 
     "phone_home": "", 
     "phone_mobile": "", 
     "preferred_locale": "en-US", 
     "salutation": "", 
     "second_name": "", 
     "suffix": "", 
     "title": "" 
    } 
} 

當前格式

{ 
    "credentials": { 
     "username": "aaa", 
     "password": "aaa", 
     "profile": { 
      "email": "[email protected]", 
      "birthday": "2011-12-12", 
      "fax": "", 
      "first_name": "", 
      "gender": "", 
      "job_title": "", 
      "last_name": "", 
      "phone_business": "", 
      "phone_home": "", 
      "phone_mobile": "", 
      "Preferred_locale": "a", 
      "salutation": "", 
      "second_name": "", 
      "suffix": "", 
      "title": "" 
     } 
    } 
} 

當前的Json代碼

JObject JsonRegInput = new JObject(
    new JProperty("credentials", 
     new JObject(
      new JProperty("username", loginData.username = username), 
      new JProperty("password", loginData.password = password), 
      new JProperty("profile", 
       new JObject(
        new JProperty("email", RegisterObject.email = email), 
        new JProperty("birthday", RegisterObject.birthday = birthday), 
        new JProperty("fax", RegisterObject.fax = ""), 
        new JProperty("first_name", RegisterObject.first_name = ""), 
        new JProperty("gender", RegisterObject.gender = ""), 
        new JProperty("job_title", RegisterObject.job_title = ""), 
        new JProperty("last_name", RegisterObject.last_name = ""), 
        new JProperty("phone_business", RegisterObject.phone_business = ""), 
        new JProperty("phone_home", RegisterObject.phone_home = ""), 
        new JProperty("phone_mobile", RegisterObject.phone_mobile = ""), 
        new JProperty("Preferred_locale", RegisterObject.preferred_locale = pref_local), 
        new JProperty("salutation", RegisterObject.salutation = ""), 
        new JProperty("second_name", RegisterObject.second_name = ""), 
        new JProperty("suffix", RegisterObject.suffix = ""), 
        new JProperty("title", RegisterObject.title = "")))))); 

我與輸入一起給予的註冊請求。我寫了JSON註冊格式,但我沒有得到所需的格式。告訴我必須更改代碼的位置。

在目前的格式中,我在最後得到3個閉括號。但我的要求是在最後帶兩個封閉的括號,另一個在密碼末尾。請參閱我的代碼。

回答

0

這將工作(測試)。從概念上講,您只需要將「個人資料」JProperty(及其內容)從「憑證」對象移動到外部對象。爲了方便,只需從整個語句的末尾移動兩個右括號到「密碼」JProperty之後。

JObject JsonRegInput = new JObject(
    new JProperty("credentials", 
     new JObject(
      new JProperty("username", loginData.username = username), 
      new JProperty("password", loginData.password = password))), 
    new JProperty("profile", 
     new JObject(
      new JProperty("email", RegisterObject.email = email), 
      new JProperty("birthday", RegisterObject.birthday = birthday), 
      new JProperty("fax", RegisterObject.fax = ""), 
      new JProperty("first_name", RegisterObject.first_name = ""), 
      new JProperty("gender", RegisterObject.gender = ""), 
      new JProperty("job_title", RegisterObject.job_title = ""), 
      new JProperty("last_name", RegisterObject.last_name = ""), 
      new JProperty("phone_business", RegisterObject.phone_business = ""), 
      new JProperty("phone_home", RegisterObject.phone_home = ""), 
      new JProperty("phone_mobile", RegisterObject.phone_mobile = ""), 
      new JProperty("Preferred_locale", RegisterObject.preferred_locale = pref_local), 
      new JProperty("salutation", RegisterObject.salutation = ""), 
      new JProperty("second_name", RegisterObject.second_name = ""), 
      new JProperty("suffix", RegisterObject.suffix = ""), 
      new JProperty("title", RegisterObject.title = ""))));