【问题标题】:generating a number between a range using json使用json生成范围之间的数字
【发布时间】:2015-03-05 00:36:33
【问题描述】:

我们如何使用 Json 生成范围之间的数字。

就像我们必须生成一个介于 0 到 50 之间的数字,我们如何使用 Json 在 Java 中执行此操作。

这是我的 Json 数据

{
  "rand": {
    "type': "number",
    "minimum": 0,
    "exclusiveMinimum": false,
    "maximum": 50,
    "exclusiveMaximum": true
  }
}

这是我在 Java 中尝试过的

public class JavaApplication1 {
public static void main(String[] args) {
    try {
                for (int i=0;i<5;i++)
                {
        FileInputStream fileInputStream = new FileInputStream("C://users/user/Desktop/V.xls");
        HSSFWorkbook workbook = new HSSFWorkbook(fileInputStream);
        HSSFSheet worksheet = workbook.getSheet("POI Worksheet");
        HSSFRow row1 = worksheet.getRow(0);


                    String e1Val = cellE1.getStringCellValue();
                    HSSFCell cellF1 = row1.getCell((short) 5);

                    System.out.println("E1: " + e1Val);

                    JSONObject obj = new JSONObject();


                    obj.put("value", e1Val);
                    System.out.print(obj + "\n");

                    Map<String,Object> c_data = mapper.readValue(e1Val, Map.class);

                    System.out.println(a);

                    }
            } catch (FileNotFoundException e) {
    } catch (IOException e) {
    }
}
}

Json 数据存储在 excel 表中,从那里我在 Java 程序中读取它

【问题讨论】:

  • JSON 是一种数据格式,而不是一种编程语言。你真正想做什么?
  • 好吧,服务器应该parse the JSON string,处理值,generate a number,然后重新编码。
  • @KenY-N 我正在用 java 编码,但使用 json 作为数据格式。我已经使用 rand: { type: 'number', minimum: 0, exclusiveMinimum: false, maximum: 50, exclusiveMaximum: true } 作为我的数据,现在我想从这里读取一个值到我的 java 程序中
  • 请给我们看代码。
  • @KenY-N 我的代码显示错误,无法读取数字

标签: java json


【解决方案1】:

获取像 GSON 这样的 Json 阅读器。 将 JSON 读入一个等效的对象,例如

public class rand{

  private String type;
  private int minimum;
  private boolean exclusiveMinimum;
  private int maximum;
  private boolean exclusiveMaximum;

  //this standard-constructor is needed for the JsonReader
  public rand(){

  }

  //Getter for all Values


}

在阅读您的 JSON 后,您可以通过您的 getter 方法访问您的数据

【讨论】:

    【解决方案2】:

    我认为Jackson 在这里可能会有所帮助。

    我建议您在 Java 中创建一个反映 JSON 的数据模型。这可以是:

    // This is the root object. It contains the input data (RandomizerInput) and a
    // generate-function that is used for generating new random ints. 
    public class RandomData {
        private RandomizerInput input;
    
        @JsonCreator
        public RandomData(@JsonProperty("rand") final RandomizerInput input) {
            this.input = input;
        }
    
        @JsonProperty("rand")
        public RandomizerInput getInput() {
            return input;
        }
    
        @JsonProperty("generated")
        public int generateRandomNumber() {
            int max = input.isExclusiveMaximum() 
                          ? input.getMaximum() - 1 : input.getMaximum();
            int min = input.isExclusiveMinimum() 
                          ? input.getMinimum() + 1 : input.getMinimum();
    
            return new Random().nextInt((max - min) + 1) + min;
        }
    }
    
    // This is the input data (pretty much what is described in the question).
    public class RandomizerInput {
        private final boolean exclusiveMaximum;
        private final boolean exclusiveMinimum;
        private final int maximum;
        private final int minimum;
        private final String type;
    
        @JsonCreator
        public RandomizerInput(
                @JsonProperty("type") final String type,
                @JsonProperty("minimum") final int minimum,
                @JsonProperty("exclusiveMinimum") final boolean exclusiveMinimum,
                @JsonProperty("maximum") final int maximum,
                @JsonProperty("exclusiveMaximum") final boolean exclusiveMaximum) {
    
            this.type = type; // Not really used...
            this.minimum = minimum;
            this.exclusiveMinimum = exclusiveMinimum;
            this.maximum = maximum;
            this.exclusiveMaximum = exclusiveMaximum;
        }
    
        public int getMaximum() {
            return maximum;
        }
    
        public int getMinimum() {
            return minimum;
        }
    
        public String getType() {
            return type;
        }
    
        public boolean isExclusiveMaximum() {
            return exclusiveMaximum;
        }
    
        public boolean isExclusiveMinimum() {
            return exclusiveMinimum;
        }
    }
    

    要使用这些类,来自 Jackson 的 ObjectMapper 可以这样使用:

    public static void main(String... args) throws IOException {
        String json =
                "{ " +
                    "\"rand\": { " +
                            "\"type\": \"number\", " +
                            "\"minimum\": 0, " +
                            "\"exclusiveMinimum\": false, " +
                            "\"maximum\": 50, " +
                            "\"exclusiveMaximum\": true " +
                    "} " +
                "}";
    
        // Create the mapper
        ObjectMapper mapper = new ObjectMapper();
    
        // Convert JSON to POJO
        final RandomData randomData = mapper.readValue(json, RandomData.class);
    
        // Either you can get the random this way...
        final int random = randomData.generateRandomNumber();
    
        // Or, you can serialize the whole thing as JSON....
        String str = mapper.writeValueAsString(randomData);
    
        // Output is:
        // {"rand":{"type":"number","minimum":0,"exclusiveMinimum":false,"maximum":50,"exclusiveMaximum":true},"generated":21}
        System.out.println(str);
    }
    

    随机数的实际生成是基于这个SO question

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-02-20
      • 1970-01-01
      • 2011-09-20
      • 1970-01-01
      • 2014-03-31
      • 2011-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多