如何更改Json对象

时间:2013-10-18 07:29:43

标签: json.net

必填格式

{
    "credentials": {
        "username": "foobar2",
        "password": "Demandware1"
    },
    "profile": {
        "email": "dude@demandware.com",
        "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": "admin1@photon.com",
            "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个封闭括号。但我的要求是在最后带两个封闭的括号,另一个在密码的末尾。请看我的代码。

1 个答案:

答案 0 :(得分:0)

这将起作用(测试)。从概念上讲,您只需要将“profile”JProperty(及其内容)从“credentials”对象移动到外部对象。要轻松完成此操作,只需将两个右括号从整个语句的末尾移到“密码”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 = ""))));