DEV Community

Cover image for JsonFormat is here
liu yang
liu yang

Posted on

JsonFormat is here

JSON Format Plugin

Introduction

The JSON Format plugin is a powerful tool for JSON data manipulation and conversion. It supports multiple IDEs, including IDEA, Android Studio, and DevEco Studio, making it a versatile addition to your development workflow.

Features

  1. JSON to JavaBean: Convert JSON data into a JavaBean class.
  2. JSON to Harmony .ets Model: Transform JSON data into a Harmony .ets file.
  3. JavaBean to Harmony .ets Model: Convert existing JavaBean classes into Harmony .ets files.

Version History

Here's a summary of the plugin's version updates and compatibility:

Plugin Version IDEA Version DevEco Version Android Studio Version Notes
v1.0.2 2023.3.1 5.0.3.403 Android Studio Jellyfish 2023.3.1
v1.0.3 2023.3.1 5.0.3.403 Android Studio Jellyfish 2023.3.1
v1.0.4-deveco 2023.3.1 5.0.3.501 Android Studio Jellyfish 2023.3.1 Deprecated
v1.0.5-deveco 2023.3.1 5.0.3.502 Android Studio Jellyfish 2023.3.1 Supports DevEco marketplace download
v1.0.6 2023.3.1 5.0.3.502 Android Studio Jellyfish 2023.3.1 Supports DevEco marketplace download

Notes

  • The plugin versions with the suffix "deveco" are specially designed for DevEco Studio. Please do not use them in IDEA or Android Studio.
  • Starting from version v1.0.6, the plugin supports all three conversion formats and is compatible with IDEA, Android Studio, and DevEco Studio.

Installation

You can find the plugin for download at plugins.jetbrains.com/plugin/2493. For Harmony IDE, follow these steps to install the plugin:

  1. Open DevEco Studio and navigate to Settings > Plugins.
  2. Click on "Install Plugin from Disk."
  3. Locate the downloaded plugin file with a .jar extension and select it.
  4. Click OK to start the installation process.
  5. Once the installation is complete, restart your IDE.

Usage

Converting JSON to Harmony .ets Model

  1. Create a new class file, such as User.ets.
  2. Right-click in the editor and select Generate, then choose Json2ets. You can also use the shortcut keys Alt + Insert to quickly access the Json2ets option.
  3. Paste your JSON string into the dialog box, enter a class name that matches the file name, and select whether to generate a class or interface.
  4. Click OK to generate the corresponding .ets model.

Examples

Simple JSON

JSON:

{
    "msg": "Success",
    "code": 200
}
Enter fullscreen mode Exit fullscreen mode

Generated Code:

export class User {
  msg?: string;
  code?: number;
}
Enter fullscreen mode Exit fullscreen mode

Nested JSON

JSON:

{
    "msg": "Success",
    "code": 200,
    "data": {
        "username": "John Doe",
        "password": "123456"
    }
}
Enter fullscreen mode Exit fullscreen mode

Generated Code:

export class User {
  msg?: string;
  code?: number;
  data?: User.Data;
}

export namespace User {
  export class Data {
    username?: string;
    password?: string;
  }
}
Enter fullscreen mode Exit fullscreen mode

JSON with List

JSON:

{
    "msg": "Success",
    "code": 200,
    "data": [
        {
            "id": 12,
            "name": "Senior Android Engineer",
            "cname": " Toutiao",
            "size": "Series D",
            "salary": "40K-60K",
            "username": "Kimi",
            "title": "HR",
            "page": "0"
        },
        {
            "id": 13,
            "name": "Mobile Architect",
            "cname": "Yinhuan Games",
            "size": "Series B",
            "salary": "15K-20K",
            "username": "Liu Li",
            "title": "Personnel Manager",
            "page": "0"
        },
        {
            "id": 14,
            "name": "Java Engineer",
            "cname": "37 Entertainment",
            "size": "Series D",
            "salary": "25K-30K",
            "username": "Reiki",
            "title": "HR-M",
            "page": "0"
        },
        {
            "id": 16,
            "name": "iOS Engineer",
            "cname": "Yinhuan Games Technology",
            "size": "Series D",
            "salary": "15K-20K",
            "username": "Ma Xiaoling",
            "title": "Java Programmer",
            "page": "0"
        },
        {
            "id": 17,
            "name": "Java Engineer",
            "cname": "4399 Games Technology",
            "size": "Series C",
            "salary": "15K-20K",
            "username": "Ma Xiaoling",
            "title": "HR-M",
            "page": "0"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Generated Code:

export class User {
  msg?: string;
  code?: number;
  data?: User.Data[];
}

export namespace User {
  export class Data {
    id?: number;
    name?: string;
    cname?: string;
    size?: string;
    salary?: string;
    username?: string;
    title?: string;
    page?: string;
  }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.