标签:style http 使用 os strong 文件 数据 io
1.关于本文
这几天打算写一个工具类JsonXmlHelper,用来进行用XML来保存JSON格式文件的工作。该工具类中要实现2个最主要的函数:
1)将JSON格式的内容写入到地址为address的XML中:WriteJsonToXml
2)把函数1中构造的XML文件恢复成JSON格式文档:RecoverJsonFromXml
函数1的实现将在本文中给出,函数2的实现将在以后发表的博文中给出
2.代码说明
1)添加引用:Newtonsoft.Json.dll
2)导入库
‘JSON解析相关函数,需要添加引用Newtonsoft.Json.dll Imports Newtonsoft.Json Imports Newtonsoft.Json.Linq ‘XMl解析相关函数 Imports System.Xml
3)在模块ModuleTest中建立类JsonXmlHelper
4)两个基础函数:
WriteToFile(将一个字符串的内容写入到文件)
ReadFromFile(从文件中读取内容赋值到一个字符串)
‘‘‘ <summary> ‘‘‘ 将字符串中内容写入到文件 ‘‘‘ </summary> ‘‘‘ <param name="address">文件地址</param> ‘‘‘ <param name="content">被写入文件的字符串</param> ‘‘‘ <remarks></remarks> Public Shared Sub WriteToFile(address As String, content As String) Try Dim sw As IO.StreamWriter sw = New IO.StreamWriter(address, False, System.Text.Encoding.UTF8) sw.Write(content) sw.Close() sw.Dispose() Catch ex As Exception Throw New Exception(ex.Message) End Try End Sub ‘‘‘ <summary> ‘‘‘ 从文件中读取信息到字符串 ‘‘‘ </summary> ‘‘‘ <param name="address">文件地址</param> ‘‘‘ <returns>读取到的字符串</returns> ‘‘‘ <remarks></remarks> Public Shared Function ReadFromFile(address As String) Try Dim sr As IO.StreamReader sr = IO.File.OpenText(address) Return sr.ReadToEnd() Catch ex As Exception Throw New Exception(ex.Message) End Try End Function
5)函数WriteJsonToXml的实现
‘‘‘ <summary> ‘‘‘ 将JSON格式的内容写入到地址为address的XML中 ‘‘‘ </summary> ‘‘‘ <param name="address">要存入的文件地址</param> ‘‘‘ <param name="json">源JSON字符串</param> ‘‘‘ <remarks></remarks> Public Shared Sub WriteJsonToXml(address As String, json As String) ‘如果存在同名文件则先删除 If IO.File.Exists(address) Then IO.File.Delete(address) End If ‘建立XML文档 Dim writer As XmlTextWriter = New XmlTextWriter(address, Text.Encoding.GetEncoding("GBK")) writer.Formatting = Xml.Formatting.Indented writer.WriteStartDocument() writer.WriteComment("这个XML文档中存储了一个JSON格式的信息") ‘递归遍历JSON中的项并写入到XML中 writer.WriteStartElement("Root") ‘将JSON字符串中内容写入到XML文档 WriteToXml(writer, json) writer.WriteEndElement() ‘Root结点结束 writer.WriteEndDocument() writer.Close() ‘XML文档创建结束 End Sub ‘‘‘ <summary> ‘‘‘ 将一段JSON格式字符串写入到XML文件中 ‘‘‘ </summary> ‘‘‘ <param name="writer"></param> ‘‘‘ <param name="json"></param> ‘‘‘ <remarks></remarks> Private Shared Sub WriteToXml(writer As XmlTextWriter, json As String) Dim jobj As JObject = JObject.Parse(json) ‘遍历读取到的JSON字符串 For Each jtemp As JProperty In jobj.Children ‘根据读入的不同数据类型分类讨论 ‘Console.WriteLine(jtemp.Name.ToString & " " & jtemp.Value.ToString) Select Case jtemp.Value.Type Case JTokenType.Object writer.WriteStartElement(jtemp.Name.ToString(), "") writer.WriteAttributeString("Type", "Object") WriteToXml(writer, jtemp.Value.ToString) writer.WriteEndElement() Case JTokenType.Array writer.WriteStartElement(jtemp.Name.ToString(), "") writer.WriteAttributeString("Type", "Array") ‘遍历数组读取值 For i As Integer = 0 To jtemp.Value.Count - 1 writer.WriteAttributeString("Value" & i, jtemp.Value(i).ToString) Next writer.WriteEndElement() Case JTokenType.String writer.WriteStartElement(jtemp.Name.ToString(), "") writer.WriteAttributeString("Type", "String") writer.WriteAttributeString("Value", jtemp.Value.ToString) writer.WriteEndElement() Case JTokenType.Boolean writer.WriteStartElement(jtemp.Name.ToString(), "") writer.WriteAttributeString("Type", "Boolean") writer.WriteAttributeString("Value", jtemp.Value.ToString) writer.WriteEndElement() Case JTokenType.Integer writer.WriteStartElement(jtemp.Name.ToString(), "") writer.WriteAttributeString("Type", "Integer") writer.WriteAttributeString("Value", jtemp.Value.ToString) writer.WriteEndElement() Case JTokenType.Float writer.WriteStartElement(jtemp.Name.ToString(), "") writer.WriteAttributeString("Type", "Float") writer.WriteAttributeString("Value", jtemp.Value.ToString) writer.WriteEndElement() Case JTokenType.Null writer.WriteStartElement(jtemp.Name.ToString(), "") writer.WriteAttributeString("Type", "Null") writer.WriteAttributeString("Value", jtemp.Value.ToString) writer.WriteEndElement() Case Else End Select Next End Sub
3.上面若干函数的调用示例
1)建立结构Person
Public Class Person Public Name As String ‘姓名 Public Age As Integer ‘年龄 Public Sex_is_Male As Boolean ‘性别 Public Structure PartnerInfo ‘伙伴信息结构 Public Partner_Name As String ‘伙伴姓名 Public Partner_Age As Integer ‘伙伴年龄 Public Partner_Sex_is_Male As Boolean ‘伙伴性别 End Structure Public Partner As PartnerInfo ‘伙伴 Public Achievement As String() ‘‘‘ <summary> ‘‘‘ 构造函数 ‘‘‘ </summary> ‘‘‘ <param name="isDefault">true:使用默认值,false:使用测试值</param> ‘‘‘ <remarks></remarks> Public Sub New(Optional ByVal isDefault As Boolean = True) Me.Name = IIf(isDefault, "", "Tsybius") Me.Age = IIf(isDefault, 0, 23) Me.Sex_is_Male = IIf(isDefault, True, True) Me.Partner.Partner_Name = IIf(isDefault, "", "Galatea") Me.Partner.Partner_Age = IIf(isDefault, 0, 21) Me.Partner.Partner_Sex_is_Male = IIf(isDefault, True, False) Me.Achievement = IIf(isDefault, New String() {}, New String() {"ach1", "ach2", "ach3"}) End Sub End Class
2)在Main函数中调用本文中的函数
Sub Main() ‘将测试内容写入到文件test.txt中 JsonXmlHelper.WriteToFile("test.txt", "这是一段测试内容") ‘从文件test.txt中读入内容 Console.WriteLine(JsonXmlHelper.ReadFromFile("test.txt")) Console.WriteLine() ‘建立Person结构实例 Dim p As Person = New Person(False) ‘将Person类型实例存储到json格式的字符串中(两种格式:不缩进、缩进) Dim json1 As String = JsonConvert.SerializeObject(p, Newtonsoft.Json.Formatting.None) Dim json2 As String = JsonConvert.SerializeObject(p, Newtonsoft.Json.Formatting.Indented) ‘输出生成的字符串 Console.WriteLine(json1 & vbCrLf & vbCrLf & json2) JsonXmlHelper.WriteJsonToXml("x.xml", json1) Console.ReadLine() End Sub
3)运行结果
控制台输出结果
生成的x.xml内容
<?xml version="1.0" encoding="gb2312"?> <!--这个XML文档中存储了一个JSON格式的信息--> <Root> <Name Type="String" Value="Tsybius" /> <Age Type="Integer" Value="23" /> <Sex_is_Male Type="Boolean" Value="True" /> <Partner Type="Object"> <Partner_Name Type="String" Value="Galatea" /> <Partner_Age Type="Integer" Value="21" /> <Partner_Sex_is_Male Type="Boolean" Value="False" /> </Partner> <Achievement Type="Array" Value0="ach1" Value1="ach2" Value2="ach3" /> </Root>
END
VB.NET 将JSON格式的字符串保存到XML文件中,布布扣,bubuko.com
标签:style http 使用 os strong 文件 数据 io
原文地址:http://my.oschina.net/Tsybius2014/blog/295334