使用json在范围之间生成数字

时间:2015-01-07 06:57:13

标签: java json

我们如何使用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 Data存储在excel表中,我在那里用Java程序

阅读它

2 个答案:

答案 0 :(得分:0)

获取像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-methods

访问您的数据

答案 1 :(得分:0)

我认为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;
    }
}

要使用这些类,杰克逊的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