Android与C#里自定义HashTable与Json转换

前言

最近自己在做的一个程序,后端用的.net C#,其中有相关的一些数据加了自定久的属性,所以用到了HashTable,由于安卓端与后端通信用到的Json数据,所以这部分东西也用到HashTable。以前的程序经常会用到HashMap,只不过没做到通守HashMap转换为Json,所以这次做个Demo看看效果。

C#

我们用VS2017新建一个C#的桌面应用程序HashTableJson,然后在管理Nuget里面添加Newtonsoft.Json,如下图

然后在窗体里加两个个按钮(一个生成Json,一个解析Json),和一个TextBox的文本框,然后我们建一个测试的类CHashDemo

然后在窗体事件里面加上生成和解析的代码

        //生成Json
        private void button1_Click(object sender, EventArgs e)
        {
            CHashDemo hashDemo=new CHashDemo();
            hashDemo.democode = "code01";
            hashDemo.demoname = "name01";
            hashDemo.democs = 1;
            //往哈希表里填数据
            hashDemo.demoht=new Hashtable();
            hashDemo.demoht.Add("ht01","valueht01");
            hashDemo.demoht.Add("ht02","valueht02");


            string json = JsonConvert.SerializeObject(hashDemo);


            textBox1.Text = json;
        }




        //解析Json
        private void button2_Click(object sender, EventArgs e)
        {
            if(textBox1.Text.Length<=0) return;
            String json = textBox1.Text;
            CHashDemo demo = JsonConvert.DeserializeObject<CHashDemo>(json);


            textBox1.AppendText(demo.democode + "\r\n");
            textBox1.AppendText(demo.demoname + "\r\n");
            textBox1.AppendText(demo.democs + "\r\n");
            //遍历哈希表
            foreach (DictionaryEntry item in demo.demoht)
            {
                textBox1.AppendText(item.Key + ":" + item.Value+"\r\n");
            }
        }

我们看看运行的效果,当运行起来后点击Button1时我们就根据创建的类生成了Json显示出来。

再点击Button2,可以看到下面把我们HashTable里的数据也遍历显示出来了。

Android

首先在Android Studio里面创建一个新的Demo,布局文件里要一个EdtText,一个TextView,和两个Button

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/edt_text"/>


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="2"
        android:orientation="horizontal">




        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btnToJson"
            android:text="ToJson"/>


        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:id="@+id/btnfromJson"
            android:text="FromJson"/>
    </LinearLayout>
    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">




        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/tv_text"/>
    </android.support.v4.widget.NestedScrollView>


</LinearLayout>

然后我们也创建一个相同的类,这里面我们的HashTable改用了ConcurrentHashMap,有关HashTable和HashMap的可以在网上搜一下,主要还是HashMap的处理速度要比HashTable好一些。

Activity里加载控件

两个按钮处理数据的方法

        btnToJson=findViewById(R.id.btnToJson);
        btnToJson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CHashDemo hashdemo=new CHashDemo();
                hashdemo.democode="code01";
                hashdemo.demoname="name01";
                hashdemo.democs=5;
                hashdemo.demoht=new ConcurrentHashMap();
                hashdemo.demoht.put("ht01","valueht01");
                hashdemo.demoht.put("ht02", "valueht02");


                Gson g=new Gson();
                String str=g.toJson(hashdemo);
                edttext.setText(str);
            }
        });




        btnFromJson=findViewById(R.id.btnfromJson);
        btnFromJson.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str=edttext.getText().toString();
                Gson g=new Gson();
                CHashDemo hashdemo= g.fromJson(str, new TypeToken<CHashDemo>() {
                }.getType());


                StringBuilder sb=new StringBuilder();
                sb.append(hashdemo.democode + "\r\n");
                sb.append(hashdemo.demoname + "\r\n");
                sb.append(hashdemo.democs + "\r\n");
                //遍历HastMap
                for (Object map:hashdemo.demoht.entrySet()
                     ) {
                    Map.Entry<String, String> item=(Map.Entry<String, String>) map;
                    sb.append(item.getKey()+":");
                    sb.append(item.getValue() + "\r\n");
                }


                textView.setText(sb.toString());
            }
        });


我们也看一下运行效果,点击了TOJSON的按钮后在红框内生成了我们的JSON的字符串,可以看出这个字符串与我们C#生成的一样,所以用HashMap和HashTable都可以实现的。

然后我们再点击FROMJSON的按钮看看效果,遍历HashMap的数据也都完全展示出来了。

-END-

Vaccae的往期经典


OpenCV

《C++ OpenCV案例实战---卡号获取

《C++ OpenCV案例实战---卡片截取(附代码)

《C++ OpenCV透视变换---切换手机正面图片》

《C++ OpenCV实战---获取数量

《C++ OpenCV实战---利用颜色分割获取数量》


Android

《Android利用SurfaceView结合科大讯飞修改语音实别UI

《Android关于语音识别的功能实现分析(一)---结构化思维》

《Android关于语音识别的功能实现分析(二)---语义解析》

《Android根据类生成签名字符串

《Android碎片化布局fragment的实战应用


.Net C#

《C#自定义特性(Attribute)讲解与实际应用

《C#根据类生成签名字符串(附DEMO下载地址)

《C++创建动态库C#调用》

《C#与三菱PLC(型号FX2N)串口通讯类


数据库及其它

《Oracel存储过程写报表实战》

《Delphi轮播视频和图片程序(用于双屏显示程序)

《SQL随机增加销售数据的脚本编写(附脚本下载地址)


长按下方二维码关注微卡智享


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Vaccae

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值