NEWS

[C#] Convert Json to Gson class java sử dụng Http Request website jsonschema2pojo

[C#] Convert Json to Gson class java sử dụng Http Request website jsonschema2pojo
Đăng bởi: Thảo Meo - Lượt xem: 4134 15:12:01, 18/06/2020DEVEXPRESS   In bài viết

Xin chào các bạn bài viết hôm nay mình sẽ hướng dẫn các bạn cách convert dữ liệu từ Json sang Gson trong lập trình C# Winform.

[C#] Convert json string to gson class java

Cách bạn có thể truy cập vào website: jsonschema2pojo.org

Khi các bạn lập trình Android, thường các bạn sẽ tạo rất nhiều model class, các bạn có thể sử dụng cách này để tạo một tool riêng cho mình.

Giúp giảm bớt thời gian làm công việc tạo Model nhàm chán.

Website này giúp chúng ta dễ dàng convert từ chuỗi json thành các Class Java hay Scala.

jsonschema

Các bạn có thể bắt gói tin request này để viết tool riêng cho mình.

Giao diện demo ứng dụng Convert Json to Gson class object c#:

json2gson_csharp

Full source code ứng dụng c#:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Json2Gson
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void btn_paste_Click(object sender, EventArgs e)
        {
            rtfInput.Text = Clipboard.GetText();
        }

        private void btn_Copy_Click(object sender, EventArgs e)
        {
            Clipboard.SetText(rtfOutput.Text.Trim());
        }

        private async void btn_convert_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txt_Packet.Text))
            {
                MessageBox.Show("Please Enter Name Packet.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            if (string.IsNullOrEmpty(txt_ClassName.Text))
            {
                MessageBox.Show("Please Enter Name Class.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            string jsonInput = rtfInput.Text;
            if (string.IsNullOrEmpty(jsonInput))
            {
                MessageBox.Show("Please Enter Input Json.", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
        

            var result = await ConvertJson2Gson(jsonInput);
            rtfOutput.Text = result;                
           
        }

        public Task<string> ConvertJson2Gson(string jsonInput)
        {
            return Task.Run(async () =>
            {
                var url = "http://www.jsonschema2pojo.org/generator/preview";
                using (HttpClient client = new HttpClient())
                {
                    var content = new FormUrlEncodedContent(new[]
                    {
                        new KeyValuePair<string, string>("schema", jsonInput),
                        new KeyValuePair<string, string>("targetpackage", txt_Packet.Text),
                        new KeyValuePair<string, string>("classname", txt_ClassName.Text),
                        new KeyValuePair<string, string>("targetlanguage", "java"),
                        new KeyValuePair<string, string>("sourcetype", "json"),
                        new KeyValuePair<string, string>("annotationstyle", "gson"),
                        new KeyValuePair<string, string>("usedoublenumbers", "true"),
                        new KeyValuePair<string, string>("includeaccessors", "true"),
                        new KeyValuePair<string, string>("propertyworddelimiters", "- _")

                    });

                    var result = await client.PostAsync(url, content);
                    return await result.Content.ReadAsStringAsync();
                }
            });
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            txt_ClassName.Focus();
            txt_ClassName.SelectAll();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Convert Json to Gson class java sử dụng Http Request website jsonschema2pojo
Đăng bởi: Thảo Meo - Lượt xem: 4134 15:12:01, 18/06/2020DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.