前言
最近自己在做的一个程序,后端用的.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-