Difference Between String Scanning And JSON Parsing For A JSON String
Basically, I have a JSON string that could be found at the link Six Critics. When I use JSON parsing to separate fields in the string, in the asynctask onPostExecute, the fragment
Solution 1:
Instead of parsing the JSON
string manually, try using the Google's GSON
library that can be used to convert a JSON
string into an equivalent Java object.
Use jsonschema2pojo to auto-generate the POJO (Java) classes you need from your JSON
string.
Steps:
- Add
GSON
library by adding the following line to theapp/build.gradle
file:dependencies { compile 'com.google.code.gson:gson:2.4' }
- Go to jsonschema2pojo.
- Copy and paste the
JSON
string into the output box. - Select
JSON
for Source type. - Select None for Annotation style.
- Click the Preview button.
- Copy and paste the generated classes into your project.
- To automatically parse the
JSON
string use the following code:Example example = new Gson().fromJson(json, Example.class);
Post a Comment for "Difference Between String Scanning And JSON Parsing For A JSON String"