1

我有以下字符串,

字符串操作="创建用户"; 字符串公司="公司名称";

但是如何将上面的字符串转换为下面的json字符串并将这个字符串传递给服务器

{"action":"CreateUser","user":{"company":{"solutionname":"","createdon":"","companyguid":"","nextinvoicenumber":"1000","companystatus":"Active","companyname":"","solutioncode":"InvoiceASAP"},"createdon":"","userstatus":"Active","companyguid":"","addressguid":"","guid":"","firstname":"Renuka","lastname":"Shah","email":"ren40@vprex.com","username":"","password":"renuka","cellphone":"","homephone":"","officephone":"","officeextension":"","faxnumber":"","phoneid":"1","sourceid":"1","primarycontact":"Y","permissioncode":"COMPANYADMIN","address":{"addr1":"","addr2":"","addr3":"","addr4":"","addr5":"","city":"","state":"","postalcode":"","country":"","note":""}}}

任何帮助,将不胜感激。

4

2 回答 2

1

执行以下操作:http ://snipplr.com/view/53225/

String Action="CreateUser"; String company="company name";
try
    {
        JSONObject action=new JSONObject();
        JSONObject user=new JSONObject();
        action.put("action", Action);
        action.put("user", user);

        JSONObject company=new JSONObject();
        user.put("company", company);
        user.put("userstatus", "Active");

        company.put("solutionname", "");
        company.put("nextinvoicenumber", "1000");
        //.... 
    }
    catch (Exception je)
    {

    }

然后将JSONObject( action) 传递给服务器..

于 2012-07-19T11:41:19.140 回答
0

您将把值以名称-值对的形式放入 JSONObject 中。

像这样:

public void writeJSON() {
    JSONObject user = new JSONObject();
    JSONObject user2;
    user2 = new JSONObject();
    try {
        user.put("dish_id", "1");
        user.put("dish_custom", "2");
        user.put("quantity", "2");
        user.put("shared", "2");

        user2.put("dish_id", "2");
        user2.put("dish_custom", "2");
        user2.put("quantity", "4");
        user2.put("shared", "3");
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
于 2012-07-19T11:40:51.210 回答