JSON.NET动态解析(更高效的解决方案) [英] JSON.NET Dynamic Parsing (More Efficient Solution)

查看:169
本文介绍了JSON.NET动态解析(更高效的解决方案)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我用JSON.NET解析JSON数据。我有动态解析使用 JObject 类的工作,但我正在寻找一个更有效的方法。这仅仅是基础,我的最终解决方案将更加复杂,但首先的首先,我需要的基本知识。

so I'm using JSON.NET to parse JSON data. I have got dynamic parsing using JObject class to work but I'm looking for a more efficient way. This is just the basics and my final solution will be much more complicated, but first thing's first, I need to get the basics.

所以我有这样的JSON数据...

So I have this JSON data...

{
    "count":1,
    "results": [{
        "user_id":5029420,
        "login_name":"EtsyStore",
        "creation_tsz":1282269739,
        "referred_by_user_id":null,
        "feedback_info": {
            "count":3038,
            "score":100
         }
     }],
     "params": {
         "user_id":"etsystore"
     },
     "type":"User",
    "pagination":{}
}

下面是我目前的code迄今为止...我想获得的 USER_ID 的结果值对象。

Here's my current code so far...I'd like to get the value of user_id under the results object.

Dim p1 = JObject.Parse(json)
Dim p2 = JObject.Parse(p1("results").ToString.Replace("[", "").Replace("]", ""))
MsgBox(p2("user_id"))

所以我能够获得 USER_ID 的价值,但我不认为这是做的最有效的方式。是否有这样做的另一种方式?非常感谢你:)如果有人已经拥有code,C#或VB.NET都可以,但是指南将只是做对我很好,谢谢:)

So I'm able to get the value of user_id but I don't think this is the most efficient way of doing it. Is there another way of doing it? Thank you very much :) If anyone already has the code, C# or VB.NET will do, but guidelines will just do fine for me, thank you :)

推荐答案

在使用JSON解析器,你应该不需要直接操纵JSON字符串(即分析器的工作),也解析JSON不止一次(该数据已经在存储器从第一分析)。所以,你是对的,你在做什么不提取数据的最佳方式。

When using a JSON parser, you should not need to directly manipulate the JSON string (that is the job of the parser), nor parse the JSON more than once (the data is already in memory from the first parse). So you are right, what you are doing is not the best way to extract the data.

下面是你如何提取一个例子 USER_ID 并从JSON使用的 LINQ到JSON API (即JObjects / JTokens)。因为结果是JSON数组,我猜想有可能不总是只有一个,所以我会在这里使用一个循环。

Here is an example of how you can extract the user_id and other result data from the JSON using the LINQ-to-JSON API (i.e. JObjects / JTokens). Because results is an array in the JSON, I'm assuming there might not always be just one result, so I will use a loop here.

' load all the JSON data into a JObject
Dim p1 As JObject = JObject.Parse(json)

' loop over the "results" array to get each child object (result)
For Each result As JToken In p1("results").Children()

    ' extract the data from each result
    Dim userId As Integer = result("user_id").Value(Of Integer)()
    Dim loginName As String = result("login_name").ToString()
    Dim creationTsz As Long = result("creation_tsz").Value(Of Long)()

    ' the feedback info is one level further down
    Dim feedback As JToken = result("feedback_info")
    Dim feedbackCount As Integer = feedback("count").Value(Of Integer)()
    Dim feedbackScore As Integer = feedback("score").Value(Of Integer)()

    ' do something with the data; perhaps write it to the console
    Console.WriteLine("User ID: " + userId.ToString())
    Console.WriteLine("Login Name: " + loginName.ToString())
    Console.WriteLine("Creation TSZ: " + creationTsz.ToString())
    Console.WriteLine("Feedback Count: " + feedbackCount.ToString())
    Console.WriteLine("Feedback Score: " + feedbackScore.ToString())
    Console.WriteLine()

Next

这篇关于JSON.NET动态解析(更高效的解决方案)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆