如何使用newtonsoft或其他json解析器简化此代码? [英] How to simplify this code using newtonsoft or other json parsers?
问题描述
我有一个简单的json
I got a simple json
{"200567175963759": { "pair": "esp_btc", "type": "sell", "amount": 2000000, "rate": 1E-08, "timestamp_created": "1498114417", "status": 0}}
我想解析它而不创建任何新类.我想让它变得简单.
I want to parse it without creating any new classes. I want to make it easy.
我正在使用创建自己的jsonhelper类来对其进行解析.基本上,它试图找到两个双引号之间的第一件事,并得到200567175963759,这是订单ID. Get参数只是在"pair":和"
I am using jsonhelper class that I created my self to parse it. It's basically try to find the first thing between two double quotes and got 200567175963759 which is the order id. Get parameter is simply finding something between "pair":" and "
对于简单的json,它可以正常工作.我该如何使用更好的解析器(例如newtonsoft)来获取订单ID(例如200567175963759或时间戳).
For simple json it works fine. How can I do get order ID, which si 200567175963759, or timestamp, using better parser, like newtonsoft.
我想知道是否可以使用newtonsoft json做到这一点?
I wonder if I can do that using newtonsoft json?
Dim jsonstring = jsonHelper.stripWhiteSpace(order3.ToString) '{"200567175963759": { "pair": "esp_btc", "type": "sell", "amount": 2000000, "rate": 1E-08, "timestamp_created": "1498114417", "status": 0}}
Dim orderid = fGetToken(order3.ToString, 1, """", """")
Dim base = b
Dim quote = key
Dim typeOfOrder = jsonHelper.getParameter(jsonstring, "type")
Dim amount = jsonHelper.getParameter(jsonstring, "amount")
Dim rate = jsonHelper.getParameter(jsonstring, "rate")
Dim timestamp_created = jsonHelper.getParameter(jsonstring, "timestamp_created")
Dim order4 = OrdersAtExchange.createOrders(amount, base, quote, _exchange, timestamp_created, rate, orderid)
_orders.Add(order4)
如果我尝试使用newtonsoft进行解析,则会得到该对象,其类型为
If I try to parse that using newtonsoft, I got this object whose type is
Dim order = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonorders)
Dim order1 = CType(order, Newtonsoft.Json.Linq.JObject)
Dim order2 = order1.Item("return").ToList
我查看了Newtonsoft.Json.Linq.JObject中的所有方法,找不到任何能将json中的字典结构转换为generic.dictionary的东西
I look at all the method in Newtonsoft.Json.Linq.JObject I can't find anything that say convert dictionary structures in json to say generic.dictionary
有类似的东西.我尝试过,但是根本没用.
There is something like that. I tried but simply didn't work.
所以我想知道是否存在使用newtonsoft解析该简单json的代码的实际示例?
So I wonder if there's an actual sample of some code parsing that simple json with newtonsoft?
推荐答案
对象是类型字典,以防属性看起来像索引或键,可能是字典
Object is Type Dictionary, In case if property looks like index or key it probably dictionary
Dim JsonString As String = "{""200567175963759"": { ""pair"": ""esp_btc"", ""type"": ""sell"", ""amount"": 2000000, ""rate"": 1E-08, ""timestamp_created"": ""1498114417"", ""status"": 0}}"
Dim JsonSettings = New Newtonsoft.Json.JsonSerializerSettings
JsonSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore
Dim OutObject = Newtonsoft.Json.JsonConvert.DeserializeObject(Of Dictionary(Of String, SampleType))(JsonString)
Class SampleType
Property pair As String
Property type As String
Property amount As String
Property rate As String
Property timestamp_created As String
Property status As String
End Class
这篇关于如何使用newtonsoft或其他json解析器简化此代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!