2017-02-16 10 views
3

が不足して、私の問題をテストするための単純なJavaコードです:奇妙なJSONマッピング問題 - ここでは要素

ObjectMapper mapper = new ObjectMapper(); 
String s = "[\n" + 
      " {\n" + 
      "  \"id\": \"\",\n" + 
      "  \"name\": \"fsgh\",\n" + 
      "  \"email\": \"[email protected]\",\n" + 
      "  \"password\": \"fdg\"\n" + 
      " },\n" + 
      " {\n" + 
      "  \"id\": \"\",\n" + 
      "  \"name\": \"sdfg\",\n" + 
      "  \"email\": \"[email protected]\",\n" + 
      "  \"password\": \"dfghfgh\"\n" + 
      " }\n" + 
      " ]"; 
String jsonBody = "{\n" + 
      " \"id\": \"\",\n" + 
      " \"name\": \"<cxzzx\",\n" + 
      " \"email\": \"[email protected]\",\n" + 
      " \"address\": \"asd 72b\",\n" + 
      " \"zip\": \"1234\",\n" + 
      " \"city\": \"Asdf\",\n" + 
      " \"country\": \"Norway\",\n" + 
      " \"enabled\": \"true\",\n" + 
      " \"quota\": \"50\",\n" + 
      " \"expires\": \"2021-04-02\",\n" + 
      " \"adminAccounts\": " + 
      s + 
      "}"; 
Set<Account> accounts = mapper.readValue(s, Set.class); 
Organization organization = mapper.readValue(jsonBody, Organization.class); 

今では私たちは2つの管理者アカウントを持っている必要がありJSONとアカウントオブジェクトから見ることができています正しいが、組織には最初のものしかない。デバッガの値のスクリーンショットを以下に示します。 From debugger

これはどこから来たのでしょうか?

+0

をどのようにadminaccountは(または任意のクラスが呼び出される)の等号/のhashCodeが実装されていますか?私は両方のアカウントに空のIDがあることに気づいた。アカウントの等価性がidに基づいている場合、HashSetは重複として(正しく)1つを省略します。 – yshavit

+0

@yshavit Accountクラスでは実装されていません。両方の要素が(正しく)設定されている「accounts」セットに対しても有効です。 – grekier

+0

ああ、良い点です。 adminAccountの1つの要素はどのようなものですか?それは何が起こっているかについて何か手がかりを与えますか? – yshavit

答えて

0

NOT文字列連結を使用することを強くお勧めします。これは、jacksonがpojosでこれを実現する可能性を提供するためです。そのクリーンでエラーの発生が少ない。

まず、組織のために、アカウントのため

public class Account { 

private String id; 

private String name; 

private String email; 

private String password; 


public Account(String id, String name, String email, String password) { 
    this.id = id; 
    this.name = name; 
    this.email = email; 
    this.password = password; 
} 

public String getId() { 
    return id; 
} 

public void setId(String id) { 
    this.id = id; 
} 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public String getEmail() { 
    return email; 
} 

public void setEmail(String email) { 
    this.email = email; 
} 

public String getPassword() { 
    return password; 
} 

public void setPassword(String password) { 
    this.password = password; 
} 
} 

をPOJOを宣言します。ここに管理者アカウントのリストが含まれています。

次に、実際のビジネスロジックになります。あなたは

Account account1 = new Account("1", "name1", "email1", "pass1"); 
Account account2 = new Account("2", "name2", "email2", "pass2"); 

を必要とするアカウントの任意の数を作成し、組織基本的に

Organization organization = new Organization("orgId", "orgEmail", "orgName", "orgAddress", "OrgZip", "orgCity", "orgCountry", true, 50, "2017-3-3", accounts); 

List<Account> accounts = new ArrayList<Account>(); 
accounts.add(account1); 
accounts.add(account2); 

次に、リストに追加し、それらを追加し、あなたのString sが口座で表されます組織によって代表されるjsonBody。これら2つのアカウントpojosを最初に作成し、組織化してJavaオブジェクトとして操作しようとしてください。

0

これを動作させるためにテストしたコードは次のとおりです。私はjackson-databind-2.8.6を使用しています。私は奇妙な何かがあなたのPOJOクラスでありまたは使用しているジャクソンのバージョンにバグがあるかもしれない疑いがある:

public class JacksonTest { 

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { 
     final ObjectMapper mapper = new ObjectMapper(); 
     final String s = "[\n" + 
     " {\n" + 
     "  \"id\": \"\",\n" + 
     "  \"name\": \"fsgh\",\n" + 
     "  \"email\": \"[email protected]\",\n" + 
     "  \"password\": \"fdg\"\n" + 
     " },\n" + 
     " {\n" + 
     "  \"id\": \"\",\n" + 
     "  \"name\": \"sdfg\",\n" + 
     "  \"email\": \"[email protected]\",\n" + 
     "  \"password\": \"dfghfgh\"\n" + 
     " }\n" + 
     " ]"; 
     final String jsonBody = "{\n" + 
     " \"id\": \"\",\n" + 
     " \"name\": \"<cxzzx\",\n" + 
     " \"email\": \"[email protected]\",\n" + 
     " \"address\": \"asd 72b\",\n" + 
     " \"zip\": \"1234\",\n" + 
     " \"city\": \"Asdf\",\n" + 
     " \"country\": \"Norway\",\n" + 
     " \"enabled\": \"true\",\n" + 
     " \"quota\": \"50\",\n" + 
     " \"expires\": \"2021-04-02\",\n" + 
     " \"adminAccounts\": " + s + 
     "}"; 

     final Set<Account> accounts = mapper.readValue(s, mapper.getTypeFactory().constructCollectionType(Set.class, Account.class)); 
     System.out.println("Accounts Size: " + accounts.size()); 
     System.out.println("Accounts: " + accounts); 

     System.out.println(); 

     final Organization organization = mapper.readValue(jsonBody, Organization.class); 
     System.out.println("Organization Admin Accounts Size: " + organization.adminAccounts.size()); 
     System.out.println("Organization Admin Accounts: " + organization.adminAccounts); 
    } 

    private static class Account { 
     @Override 
     public String toString() { 
     return "Account [id=" + this.id + ", name=" + this.name + ", email=" + this.email + ", password=" + this.password + "]"; 
     } 

     public String id, name, email, password; 
    } 

    private static class Organization { 
     @Override 
     public String toString() { 
     return "Organization [id=" + this.id + ", name=" + this.name + ", email=" + this.email + ", address=" + this.address + ", zip=" + 
      this.zip + ", city=" + 
      this.city + ", country=" + this.country + ", enabled=" + this.enabled + ", quota=" + this.quota + ", expires=" + this.expires + 
      ", adminAccounts=" + 
      this.adminAccounts + "]"; 
     } 

     public String id, name, email, address, zip, city, country, enabled, quota, expires; 
     public Set<Account> adminAccounts; 
    } 
} 
0

私はsのために、この配列は、一つだけの要素を持って、それを得る、S =「[ object1] "とobject1には2つの要素があります。したがって、accounts.size()= 2、organization.adminAccounts = s =" [object1] "、organization.adminAccounts.size()= 1、organization.adminAccountsにはobject1があります。 organization.adminAccounts.size()= 2、organization.adminAccounts = "[[object2]、[object3]]" s = "[[object2]、[object3]]"が必要です。 あなたがこの試すことができます:

String s = "[\n" + 
     " [{\n" + 
     "  \"id\": \"\",\n" + 
     "  \"name\": \"fsgh\",\n" + 
     "  \"email\": \"[email protected]\",\n" + 
     "  \"password\": \"fdg\"\n" + 
     " }],\n" + 
     " [{\n" + 
     "  \"id\": \"\",\n" + 
     "  \"name\": \"sdfg\",\n" + 
     "  \"email\": \"[email protected]\",\n" + 
     "  \"password\": \"dfghfgh\"\n" + 
     " }]\n" + 
     " ]"; 

enter image description here