2015-12-04 49 views
2

我的一個JSON內容有entityTypeId的編號,如何將其更改爲String?JSON數字字符串

例如將1改爲「1」。

JSON

[ 
{ 
entityTypeId: 3, 
entityTypeName: "Branch of Legal Entity" 
}, 
{ 
entityTypeId: 1, 
entityTypeName: "Legal Entity" 
}, 
{ 
entityTypeId: 2, 
entityTypeName: "Notional Entity" 
} 
] 

REST API

@GET 
@Path(value = "/entityTypes") 
@Produces(MediaType.APPLICATION_JSON) 
@Override 
public List<EntityType> getEntityTypes() { 
    return commonBusiness.getEntityTypes(); 
} 

JPA實體

public class EntityType implements Serializable { 
    private static final long serialVersionUID = 1L; 

    @Id 
    @Column(name="ENTITY_TYPE_ID") 
    private long entityTypeId; 

    @Column(name="ENTITY_TYPE_NAME") 
    private String entityTypeName; 

更新:

你們許多人問我爲什麼需要改變爲一個字符串。我使用這個JSON數據來呈現下拉菜單。該下拉值(entityTypeId)成功保存在數據庫中的數字列中。但是當我加載視圖頁面時,下拉列表中沒有加載該值。其他下拉式工作,它們的值均爲String。

早些時候,我提出了一個問題,分離 Angularjs - dropdown - Should the key/value pair always be a string data type

+0

接受另一個JSON對象,並在其中添加「」到它。 – Shivam

+1

你爲什麼要把它改成字符串? – Aakash

+0

@Aakash謝謝,我已經更新了這個問題,並將其作爲String的原因。 – Jay

回答

0

我想你應該讓「entityTypeId」的字符串,而不是長。

+0

謝謝,這是一個很好的和簡單的修復,但不幸的是,我們無法改變,因爲它需要匹配數據庫。 – Jay

0

我建議使用包含數據作爲字符串的DTO,並在服務層進行轉換。

0

我想你應該試試這個。 json = json.replace(/:(\ d +)([,}])/ g,':「$ 1」$ 2'); 如果您使用過json庫。

1

在您的EntityType類中,您需要將entityTypeId的類型更改爲String,但如果這樣做可能會產生影響,所以您需要考慮該列在數據庫中接受的內容。更大的問題是爲什麼你想改變你的數據類型是一個字符串。

+0

謝謝我已經更新了這個問題的原因是一個字符串。 – Jay

+1

我想我已經設法在它自己的線程中回答你的角JS問題。我認爲暫時只是爲了實現某些工作,值得創建一個屬性'entityTypeIdAsString',它只是返回一個字符串,並將其傳遞到您的頁面,然後在考慮任何干淨的方法之前查看它是否有效。 –

0

如果我理解正確,這裏的要求是讓REST服務json響應將entityType作爲一個數字。 這可以通過使用自定義序列化器創建json響應來實現。

@GET 
@Path(value = "/entityTypes") 
@Produces(MediaType.APPLICATION_JSON) 
@Override 
public Response getEntityTypes() { 
    ObjectMapper mapper = new ObjectMapper(); 
    SimpleModule module = new SimpleModule("entityModule", 
       Version.unknownVersion()); 
    module.addSerializer(EntityCollection.class, new EntityTypeJsonSerializer()); 
    mapper.registerModule(module); 
    String responseJson = mapper.writeValueAsString(commonBusiness.getEntityTypes()); 
    return Response 
        .status(Status.OK) 
        .entity(responseJson) 
        .type(MediaType.APPLICATION_JSON).build(); 
} 

創建一個臨時集合類

public class EntityCollection{ 
    private List<EntityType> entityTypes; 
} 

自定義序列:

public class EntityTypeJsonSerializer extends JsonSerializer<EntityCollection> { 

    @Override 
    public void serialize(EntityCollection entityTypes, JsonGenerator jgen, 
      SerializerProvider provider) throws IOException, 
      JsonProcessingException { 
       // JSON parsing goes here 

       jgen.writeString(String.valueOf(entityType.get(entityTypeId))); 

     } 
} 

這會讓你的獨立JPA實體和響應JSON。

0

我不認爲使用數字是一個選擇問題。

看看我創建的這個例子。

<div ng-app="myApp" ng-controller="myController"> 
    <select ng-model="entity.selected"> 
    <option value="{{ent.entityTypeId}}" ng-repeat="ent in entityTypes">{{ent.entityTypeName}}</option> 
    </select> 
    Selected entity id {{entity.selected}} 
</div> 

JSFiddle

我也更新您的其他問題。讓我知道這是你在找什麼。