Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JSON数字到字符串_Java_Json_Rest - Fatal编程技术网

Java JSON数字到字符串

Java JSON数字到字符串,java,json,rest,Java,Json,Rest,我的一个JSON内容包含entityTypeId的编号,如何将其更改为字符串 例如将1改为“1” JSON [ { entityTypeId: 3, entityTypeName: "Branch of Legal Entity" }, { entityTypeId: 1, entityTypeName: "Legal Entity" }, { entityTypeId: 2, entityTypeName: "Notional Entity" } ] RESTAPI @GET @Pa

我的一个JSON内容包含entityTypeId的编号,如何将其更改为字符串

例如将1改为“1”

JSON

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

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

你们中的许多人问我为什么需要改成字符串。我使用这个JSON数据呈现一个下拉列表。此下拉值(entityTypeId)成功保存在数据库中的数字列中。但是当我加载视图页面时,下拉列表中没有加载该值。其他下拉列表将这两个值都作为字符串使用

早些时候,我提出了一个单独的问题

我认为您应该将“entityTypeId”设置为字符串而不是长字符串。

我建议使用包含数据的DTO作为字符串,并在服务层进行转换。

我认为您应该尝试一下。json=json.replace(/:(\d+)([,}])/g,“:”$1“$2”);
如果您使用了json库。

在EntityType类中,您需要将entityTypeId的类型更改为字符串,但如果这样做可能会产生影响,因此您需要考虑该列在数据库中接受什么。更大的问题是为什么要将数据类型更改为字符串。

如果我理解正确,这里的要求是让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;
}
公共类EntityCollection{
私有列表实体类型;
}
自定义序列化程序:

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)));

        }
}
公共类EntityTypeJsonSerializer扩展JsonSerializer{
@凌驾
public void serialize(EntityCollection entityTypes、JsonGenerator jgen、,
SerializerProvider提供程序)引发IOException,
JsonProcessingException{
//JSON解析在这里进行
writeString(String.valueOf(entityType.get(entitytytypeid));
}
}

这将使您的JPA实体和响应JSON独立。

我不认为在select中使用数字是一个问题

看看我创建的这个例子

<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>

{{ent.entityTypeName}
所选实体id{{entity.Selected}


我还更新了你的另一个问题。让我知道这是否是您要找的对象。

获取另一个JSON对象并将其放入其中,并在其中添加“”。为什么要将其更改为字符串?@Aakash谢谢,我已更新了问题,并说明了将其更改为字符串的原因。谢谢,这是一个很好且简单的修复方法,但不幸的是,我们无法改变这一点,因为它需要匹配数据库。谢谢,我更新了这个问题,并说明了成为字符串的原因。我想我已经在它自己的线程中回答了你的问题。我认为暂时来说,为了让某些东西正常工作,创建一个属性“entityTypeIdAsString”是值得的,它只返回与字符串一样长的字符串,并将其插入页面,然后在考虑任何更简洁的方法之前先看看它是否有效。
<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>