Skip to content Skip to sidebar Skip to footer

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:

  1. Add GSON library by adding the following line to the app/build.gradle file: dependencies { compile 'com.google.code.gson:gson:2.4' }
  2. Go to jsonschema2pojo.
  3. Copy and paste the JSON string into the output box.
  4. Select JSON for Source type.
  5. Select None for Annotation style.
  6. Click the Preview button.
  7. Copy and paste the generated classes into your project.
  8. 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"