且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

字符串扫描和JSON字符串的JSON解析之间的区别

更新时间:2022-04-30 05:20:02

而不是手动解析JSON字符串,请尝试使用Google的GSON库,该库可用于将JSON字符串转换为等效的Java目的.

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.

使用jsonschema2pojo从JSON字符串自动生成所需的POJO(Java)类.

Use jsonschema2pojo to auto-generate the POJO (Java) classes you need from your JSON string.

步骤:

    通过在app/build.gradle文件中添加以下行来
  1. 添加GSON库:dependencies { compile 'com.google.code.gson:gson:2.4' }
  2. 转到 jsonschema2pojo .
  3. JSON字符串复制并粘贴到输出框中.
  4. 为源类型选择JSON.
  5. 选择无"作为注释样式.
  6. 单击预览"按钮.
  7. 将生成的类复制并粘贴到您的项目中.
  8. 要自动解析JSON字符串,请使用以下代码:Example example = new Gson().fromJson(json, Example.class);
  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);