1、第一步新建項目
2、第二步,新建項目后,在窗體添加一個按鈕Button和兩個富文本框RichTextBox
3、第三步,VS->工具->NuGet包管理器->管理解決方案的NuGet程序包->瀏覽->搜索(Newtonsoft)->安裝第1個即可
?
4、第四步,在項目引用里面找到Newtonsoft引用然后把屬性“復(fù)制到本地改為False”
5、第五步,在項目上面右擊鼠標打開項目屬性
6、第六步,在項目屬性->資源->添加資源->添加現(xiàn)有文件;然后在項目目錄packages->Newtonsoft.Json.13.0.1->lib->net45(根據(jù)項目框架選擇)->Newtonsoft.Json.dll選擇對應(yīng)的dll文件添加到資源里面
7、第七步,編寫測試代碼
添加要格式化的json字符串
{“status”:1,“totalcount”:2,“l(fā)ist”:[{“id”:“2305b1e2-4e31-4fd3-8eb6-db57641914df”,“code”:“8147056167227050270”,“title”:“testing”,“type”:“產(chǎn)品”,“status”:“已處理”,“datetime”:“2014-07-12T21:16:46”,“replycontent”:“好的,只是測試”},
{“id”:“3a6546f6-49a7-4a17-b679-b3812b12b27e”,“code”:“8147056167227050269”,“title”:“我建議龍頭有多種選配方式”,“type”:“產(chǎn)品”,“status”:“未處理”,“datetime”:“2014-07-12T18:49:08.933”,“replycontent”:""},
{“id”:“f735e461-ca72-4b44-8d7b-cd97ac09802f”,“code”:“8147056167227050268”,“title”:“這個產(chǎn)品不怎么好,不好用”,“type”:“產(chǎn)品”,“status”:“未處理”,“datetime”:“2014-07-12T15:06:19.1”,“replycontent”:""},
{“id”:“15926d9d-f469-4921-b01d-4b48ef8bd93d”,“code”:“7141054273018032465”,“title”:“jdjbcn”,“type”:“服務(wù)”,“status”:“未處理”,“datetime”:“2014-05-27T01:03:46.477”,“replycontent”:""},
{“id”:“1debf78f-42b3-4037-b71f-34075eed92bc”,“code”:“4141051277003536211”,“title”:“jdjbxn.x”,“type”:“服務(wù)”,“status”:“未處理”,“datetime”:“2014-05-27T00:53:21.18”,“replycontent”:""},
{“id”:“27593c52-b327-4557-8106-b9156df53909”,“code”:“1143051276001357050”,“title”:“ghggghh”,“type”:“服務(wù)”,“status”:“未處理”,“datetime”:“2014-05-27T00:35:05.933”,“replycontent”:""},
{“id”:“040198fc-b466-46c1-89d8-0514fbde9480”,“code”:“4142053251166372433”,“title”:“你好,你知道啦,我不喜歡白色浴缸”,“type”:“服務(wù)”,“status”:“未處理”,“datetime”:“2014-05-25T16:37:43.853”,“replycontent”:""},
{“id”:“16185418-d461-4e98-83c3-824eb7e344d6”,“code”:“4145058213013197148”,“title”:“hdjbchh”,“type”:“服務(wù)”,“status”:“未處理”,“datetime”:“2014-05-21T01:19:14.903”,“replycontent”:""},
{“id”:“6c043404-c1db-42e8-adeb-d4880fa7d1b5”,“code”:“0142051185128085372”,“title”:“ghhjdhd”,“type”:“服務(wù)”,“status”:“未處理”,“datetime”:“2014-05-18T12:08:37.997”,“replycontent”:""},
{“id”:“2dca1a38-a32b-4955-a99c-2ed7d6de60fa”,“code”:“3146050186122030382”,“title”:“hsibcn”,“type”:“服務(wù)”,“status”:“未處理”,“datetime”:“2014-05-18T12:03:38.913”,“replycontent”:""}]}
json格式化vb代碼
'''<summary>
'''格式化JSON字符串
'''</summary>
'''<paramname="str"></param>
'''<returns></returns>
PrivateFunctionConvertJsonString(strAsString)AsString
DimserializerAsNewJsonSerializer()
DimtrAsTextReader=NewStringReader(str)
DimjtrAsNewJsonTextReader(tr)
DimobjAsObject=serializer.Deserialize(jtr)
IfobjIsNotNothingThen
DimtextWriterAsNewStringWriter()
DimjsonWriterAsNewJsonTextWriter(textWriter)With{
.Formatting=Formatting.Indented,
.Indentation=4,
.IndentChar=""c
}
serializer.Serialize(jsonWriter,obj)
ReturntextWriter.ToString()
Else
Returnstr
EndIf
EndFunction
8、第八步,加載資源中DLL代碼重點來咯
PublicSubNew()
''加載DLL到exe的事件
AddHandlerAppDomain.CurrentDomain.AssemblyResolve,NewResolveEventHandler(AddressOfCurrentDomain_AssemblyResolve)
InitializeComponent()
EndSub
'''<summary>
'''把DLL加載到EXE中
'''</summary>
'''<paramname="sender"></param>
'''<paramname="args"></param>
'''<returns></returns>
PrivateFunctionCurrentDomain_AssemblyResolve(senderAsObject,argsAsResolveEventArgs)AsSystem.Reflection.Assembly
DimdllNameAsString=If(args.Name.Contains(","),args.Name.Substring(0,args.Name.IndexOf(","c)),args.Name.Replace(".dll",""))
dllName=dllName.Replace(".","_")
IfdllName.EndsWith("_resources")Then
ReturnNothing
EndIf
DimrmAsNewSystem.Resources.ResourceManager([GetType].Namespace&".Resources",System.Reflection.Assembly.GetExecutingAssembly())
DimbytesAsByte()=DirectCast(rm.GetObject(dllName),Byte())
ReturnSystem.Reflection.Assembly.Load(bytes)
EndFunction
9、完整實現(xiàn)代碼