-7

How can we generate a number between a range using Json.

Like we have to generate a number between 0 to 50, how can we perform this in Java using a Json.

This is my Json Data

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

This is what I have tried in 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 is stored in excel sheet, from there I am reading it in Java program

Sandeep
  • 13
  • 1
  • 2
  • 10
  • 4
    JSON is a data format, not a programming language. What are you really trying to do? – Ken Y-N Jan 07 '15 at 07:01
  • Well, the server should [parse the JSON string](http://stackoverflow.com/questions/16574482/decoding-json-string-in-java), handle the values, [generate a number](http://stackoverflow.com/questions/7219058/generate-random-integer-from-min-to-max), and encode it again. – Willem Van Onsem Jan 07 '15 at 07:04
  • @KenY-N i am coding in java but using json as the data format. I have used rand: { type: 'number', minimum: 0, exclusiveMinimum: false, maximum: 50, exclusiveMaximum: true } as my data, now i want to read a value from this into my java program – Sandeep Jan 07 '15 at 07:08
  • Show us the code, please. – Ken Y-N Jan 07 '15 at 07:19
  • @KenY-N my code is showing error and not able to read the number – Sandeep Jan 07 '15 at 07:27
  • On the web there are a million and one examples of how to parse JSON in Java. Try something out, then if you run into a problem, come back here and post a new question. – Ken Y-N Jan 07 '15 at 08:17
  • possible duplicate of [parse json structure from excel sheet into java](http://stackoverflow.com/questions/27705255/parse-json-structure-from-excel-sheet-into-java) – Ken Y-N Jan 07 '15 at 08:19

2 Answers2

0

Get a Json-reader like GSON. Read in the JSON to an equivalent Object like

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


}

and after reading in your JSON you can access your Data via your getter-methods

Naxos84
  • 1,890
  • 1
  • 22
  • 34
0

I think that Jackson may be of help here.

I suggest that you create a data model in Java that reflects the JSON. This can along the lines of:

// 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;
    }
}

To use these classes the ObjectMapper from Jackson can be used like this:

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

The actual generation of a random number is based on this SO question.

Community
  • 1
  • 1
wassgren
  • 18,651
  • 6
  • 63
  • 77