如何将字符串顺序转换为json

发布于 2025-01-11 00:21:07 字数 1785 浏览 0 评论 0原文

public void addNewUser(MongoClient mdbClient, String newUserName, String  newUserPassword, DBManagement.DBRole roles) {
      
    System.out.println("inside addNEw User method");
    Map<String, String> user = new LinkedHashMap<String, String>();
    user.put("createUser", newUserName);
    user.put("pwd", newUserPassword); 
    List<Map<String, String>> listOfRoles = new ArrayList<Map<String, String>>();
    Map<String, String> role1 = new LinkedHashMap<String, String>();
    role1.put("role",roles.getRole());
    role1.put("db", roles.getDb());
    listOfRoles.add(role1);
    user.put("roles", listOfRoles.toString());
    System.out.println("MAP: " + user);

    try{
        String json = new ObjectMapper().writeValueAsString(user);
        /*String json = new ObjectMapper().convertValue(user);*/
        System.out.println(json);
        //String jsonCommand = "{ createUser: \" + newUserName +"/" + " ," + "pwd: /" + newUserPassword + "/" + " ,"  + "roles : [" + roles_str + "]}" ;
        String jsonCommand = json;
        System.out.println("createUserString-->"+jsonCommand);
        Document command  = new Document(Document.parse(jsonCommand));
        Document collStatsResults = mdbClient.getDatabase("admin").runCommand(command);
        System.out.println(collStatsResults.toJson());
    } catch(Exception e) {
          System.out.println("Error " + e);
    }
}

我得到的输出字符串为 -{"createUser":"demoUser2","pwd":"password","roles":"[{role=dbOwner, db=udata}]"}

输出- {"createUser":"demoUser2","pwd":"密码","roles":[{"role":"dbOwner", "db":"udata"}]}

首先我使用 JSONObject() 但它不关心 json 序列,所以我尝试使用 linkedhashMap 但面临数组转换问题..任何人都可以帮忙。或者还有其他吗顺序生成json的方法。

public void addNewUser(MongoClient mdbClient, String newUserName, String  newUserPassword, DBManagement.DBRole roles) {
      
    System.out.println("inside addNEw User method");
    Map<String, String> user = new LinkedHashMap<String, String>();
    user.put("createUser", newUserName);
    user.put("pwd", newUserPassword); 
    List<Map<String, String>> listOfRoles = new ArrayList<Map<String, String>>();
    Map<String, String> role1 = new LinkedHashMap<String, String>();
    role1.put("role",roles.getRole());
    role1.put("db", roles.getDb());
    listOfRoles.add(role1);
    user.put("roles", listOfRoles.toString());
    System.out.println("MAP: " + user);

    try{
        String json = new ObjectMapper().writeValueAsString(user);
        /*String json = new ObjectMapper().convertValue(user);*/
        System.out.println(json);
        //String jsonCommand = "{ createUser: \" + newUserName +"/" + " ," + "pwd: /" + newUserPassword + "/" + " ,"  + "roles : [" + roles_str + "]}" ;
        String jsonCommand = json;
        System.out.println("createUserString-->"+jsonCommand);
        Document command  = new Document(Document.parse(jsonCommand));
        Document collStatsResults = mdbClient.getDatabase("admin").runCommand(command);
        System.out.println(collStatsResults.toJson());
    } catch(Exception e) {
          System.out.println("Error " + e);
    }
}

I am getting output string as -{"createUser":"demoUser2","pwd":"password","roles":"[{role=dbOwner, db=udata}]"}

Expected output- {"createUser":"demoUser2","pwd":"password","roles":[{"role":"dbOwner", "db":"udata"}]}

Firstly i used JSONObject() but it doesnt care about the json sequence ,so i tried with linkedhashMap but facing array conversion issue..can anyone help.Or is there any other way to generate json sequentially.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文