-1

Trying to use javax.json to read some json data. Currently I have no issues to read the file this way(exception handling skipped):

FileInputStream json =  new FileInputStream("testJSON.json");
JsonParser parser = Json.createParser(json);

This works fine when I have a seperate json file, in this case it is testJSON.json. Now what I want to do is simply parse in the json string, instead of the json file. For example, I have a json string that looks like this:

[{"Gross Weight":"12","Tare Weight":"3","Price Per Pound":"2"},{"Gross Weight":"123","Tare Weight":"32","Price Per Pound":"23"}]

Is it possible to parse this json data to the JsonParser? Or any other Java library can handle this request?

OPK
  • 4,120
  • 6
  • 36
  • 66
  • 2
    You can convert your `String` instance to an `InputStream`: see [this SO answer](http://stackoverflow.com/a/782183/1076463) – Robin Sep 01 '15 at 14:11

1 Answers1

0
java.io.StringReader in = new java.io.StringReader(theString);
JsonParser parser = Json.createParser(in);

Please study the Javadocs of the libaries you are using from time to time.

wero
  • 32,544
  • 3
  • 59
  • 84
  • `String jsonString = "[{"Gross Weight":"12","Tare Weight":"3","Price Per Pound":"2"},{"Gross Weight":"123","Tare Weight":"32","Price Per Pound":"23"}]";` When I do this I got syntax errors, what should I do to make the error go away? – OPK Sep 01 '15 at 14:19
  • Java uses `"` as delimiter for string literals. If you want to write a string literal which contains `"` you need to escape it as `\"`, e.g. `jsonString = "[{\"Gross Weight\":\"12\".... ";` – wero Sep 01 '15 at 14:23