当前位置: 首页 > 工具软件 > JSONHelper > 使用案例 >

JsonHelper 使用

欧阳博文
2023-12-01

JsonHelper类:

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DoorService
{
  public class JsonHelper
  {
    //对应JSON的singleInfo成员
    protected string _msgid = string.Empty;
    protected string _status = string.Empty;

    public ArrayList arrData = new ArrayList();
    protected ArrayList arrDataItem = new ArrayList();


    public JsonHelper()
    {

    }

    public string msgid
    {
      get
      {
        return _msgid;
      }
      set
      {
        //如设置error,则自动设置success为false
        if (value != "") _msgid = string.Empty;
        _msgid = value;
      }
    }

    public string status
    {
      get
      {
        return _status;
      }
      set
      {
        //如设置error,则自动设置success为false
        if (value != "") _status = string.Empty;
        _status = value;
      }
    }

//重置,每次新生成一个json对象时必须执行该方法
    public void Reset()
    {
      _msgid = string.Empty;
      _status = string.Empty;
      arrData.Clear();
      arrDataItem.Clear();
    }

    public void AddItem(string name, string value)
    {
      arrData.Add("\"" + name + "\":" + "\"" + value + "\"");
    }

    public void AddItem(string name, int value)
    {
      arrData.Add("\"" + name + "\":" + "" + value + "");
    }

    public void AddItem(int name, int value)
    {
      arrData.Add("\"" + name + "\":" + "\"" + value + "\"");
    }

    public void ItemOk()
    {
      arrData.Add("<BR>");
    }

    //序列化JSON对象,得到返回的JSON代码
    public string ToJson()
    {

      StringBuilder sb = new StringBuilder();
      //sb.Append("[");
      //sb.Append("\"msgid\":" + "\"" + _msgid.ToString().ToLower() + "\",");
      //sb.Append("\"status\":" + "\"" + _status.ToString().ToLower() + "\",");
      sb.Append("{");

      int index = 0;
      if (arrData.Count <= 0)
      {
        sb.Append("}");
      }
      else
      {
        foreach (string val in arrData)
        {
          index++;
          if (val != "<BR>")
          {
            sb.Append(val + ",");
          }
          else
          {
            sb = sb.Replace(",", "", sb.Length - 1, 1);
            sb.Append("},");
            if (index < arrData.Count)
            {
              sb.Append("{");
            }
          }
        }
        sb = sb.Replace(",", "", sb.Length - 1, 1);
        sb.Append("}");
      }
      //sb.Append("]");
      return sb.ToString();
    }
  }
}

使用方法:

 

UdpClient udpcSend;
JsonHelper help = new JsonHelper(); 
//IPAddress提供了对IP地址的转换、处理等功能
//IPEndPoint形成到服务的连接点
IPEndPoint localIpep = new IPEndPoint(IPAddress.Parse(myudpip), myudpport);     
udpcSend = new UdpClient(localIpep);
help.Reset(); 
help.AddItem("tableID", tableIDs); 
help.AddItem("tableName", "hkMes");
var msg = help.ToJson();
byte[] sendbytes = Encoding.UTF8.GetBytes(msg);
IPEndPoint remoteIpep = new IPEndPoint(IPAddress.Parse(youudpip), youudpport);
udpcSend.Send(sendbytes, sendbytes.Length, remoteIpep);
udpcSend.Close();  //记得关闭udpSend,否则嵌套字占用

 

转载于:https://www.cnblogs.com/HansZimmer/p/10081782.html

 类似资料: