概述
json是很方便的传值格式,在C#中用到过的转化的方法。如下:
1.自己编码进行转化:
string images = "";//新闻正文图片
StringBuilder sb = new StringBuilder();
sb.Append("[");
foreach (var item in allimage.Split(new string[]{"%D%W"}, StringSplitOptions.RemoveEmptyEntries))
{
sb.Append(""");
string temp = regex.Replace(item, "");
sb.Append(temp);
sb.Append("",");
}
sb.Append("]");
sb.Replace(",]","]");
images = sb.ToString();//最终得到自己拼接的json
2.通过别人家的类库-JsonTool
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Script.Serialization;
using System.Collections;
using System.Data;
using System.Reflection;
using System.Security.Cryptography;
using Newtonsoft.Json.Converters;
using System.Xml;
using DW.News.Manage.Comm;
namespace PM.ClientLibrary
{
public class JsonTool
{
#region DataTableToJson
/// <summary>
/// DataTableToJson
/// </summary>
/// <param name="dtb"></param>
/// <returns></returns>
public static string Dtb2Json(DataTable dtb)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
ArrayList dic = new ArrayList();
foreach (DataRow row in dtb.Rows)
{
Dictionary<string, object> drow = new Dictionary<string, object>();
foreach (DataColumn col in dtb.Columns)
{
if (row[col.ColumnName].GetType().Name == "Int64")
{
drow.Add(col.ColumnName, row[col.ColumnName].ToString());
}
else
{
drow.Add(col.ColumnName, row[col.ColumnName]);
}
}
dic.Add(drow);
}
if(DW.News.Manage.Comm.ConfigProvider.MaxJsonLength > 0)
{
jss.MaxJsonLength = DW.News.Manage.Comm.ConfigProvider.MaxJsonLength;
}
return jss.Serialize(dic);
}
#endregion
#region JsonToDataTable
/// <summary>
/// JsonToDataTable
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static DataTable Json2Dtb(string json)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
ArrayList dic = jss.Deserialize<ArrayList>(json);
DataTable dtb = new DataTable();
if (dic.Count > 0)
{
foreach (Dictionary<string, object> drow in dic)
{
if (dtb.Columns.Count == 0)
{
foreach (string key in drow.Keys)
{
dtb.Columns.Add(key, drow[key].GetType());
}
}
DataRow row = dtb.NewRow();
foreach (string key in drow.Keys)
{
if (row.Table.Columns.Contains(key))
row[key] = drow[key];
}
dtb.Rows.Add(row);
}
}
return dtb;
}
#endregion
#region JsonToDataTable
/// <summary>
/// JsonToDataTable
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static DataTable Json2Dtb(string json, DataTable dt)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
ArrayList dic = jss.Deserialize<ArrayList>(json);
DataTable dtb = new DataTable();
if (dic.Count > 0)
{
foreach (Dictionary<string, object> drow in dic)
{
if (dt == null)
{
if (dtb.Columns.Count == 0)
{
foreach (string key in drow.Keys)
{
dtb.Columns.Add(key, drow[key].GetType());
}
}
}
else
dtb = dt;
DataRow row = dtb.NewRow();
foreach (string key in drow.Keys)
{
if(row.Table.Columns.Contains(key))
row[key] = drow[key];
}
dtb.Rows.Add(row);
}
}
return dtb;
}
#endregion
#region StringToJson
/// <summary>
/// StringToJson
/// </summary>
/// <param name="dtb"></param>
/// <returns></returns>
public static string str2Json(string strInfo)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(strInfo);
}
#endregion
#region 对象转换为Json字符串
/// <summary>
/// 对象转换为Json字符串
/// </summary>
/// <param name="jsonObject">对象</param>
/// <returns>Json字符串</returns>
public static string ToJson(object jsonObject)
{
//IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
//timeFormat.DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
//return Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented, timeFormat);
Newtonsoft.Json.JsonSerializerSettings microsoftDateFormatSettings = new Newtonsoft.Json.JsonSerializerSettings();
microsoftDateFormatSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
return Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject, microsoftDateFormatSettings);
}
/// <summary>
/// 对象转换为Json字符串
/// </summary>
/// <param name="jsonObject">对象</param>
/// <param name="IsoDateTimeFormat">时间格式,如 yyyy-MM-dd HH:mm:ss.fff</param>
/// <returns>Json字符串</returns>
public static string ToJson(object jsonObject,string IsoDateTimeFormat)
{
IsoDateTimeConverter timeFormat = new IsoDateTimeConverter();
timeFormat.DateTimeFormat = IsoDateTimeFormat;
return Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented, timeFormat);
}
#endregion
#region JsonToobj
/// <summary>
/// JsonToobj
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static object Json2Obj(string json)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject(json);
}
public static T Json2Obj<T>(string json)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);
}
#endregion
#region xml转换为Json字符串
/// <summary>
/// xml转换为Json字符串
/// </summary>
/// <param name="doc">xml</param>
public static string SerializeXmlNode(XmlDocument doc)
{
return Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);
}
#endregion
#region xml转换为Json字符串
/// <summary>
/// xml转换为Json字符串
/// </summary>
/// <param name="doc">xml</param>
public static string SerializeXmlNode(XmlNode xmlnode)
{
return Newtonsoft.Json.JsonConvert.SerializeXmlNode(xmlnode);
}
#endregion
#region jquery参数解码
/// <summary>
/// jquery参数解码
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string unescape(string str)
{
str = Microsoft.JScript.GlobalObject.unescape(str);
return str;
}
#endregion
}
}
可以通过在list中放元素,然后执行ToJson方法。
–以后如果有其他的方法会再进行补充
2017年3月14日
http://blog.csdn.net/lanhai96/article/details/41041725
这篇讲的很详细,但是目前基础知识不够。待后续理解其中的方法。
最后
以上就是落后口红为你收集整理的将字符串处理成Json格式的全部内容,希望文章能够帮你解决将字符串处理成Json格式所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复