locked
parsing a linq output RRS feed

  • Question

  • I called a library (51 degrees) that returns properties about your laptop (or smartphone, or tablet), and the result it returned was this:

    {
      "MatchMethod": "Exact",
      "Difference": 0,
      "DetectionTime": 0.0,
      "Values": {
        "IsMobile": [
          "False"
        ]
      },
      "DataSetName": "Basic",
      "Published": "2015-04-01T00:00:00Z",
      "SignaturesCompared": 0,
      "ProfileIds": {
        "1": 15364,
        "2": 21460,
        "3": 21461,
        "4": 18092
      },
      "Useragent": "Mozilla/5.0 (Windows NT 6.3; WOW64  Trident            rv:11.0",
      "TargetUseragent": "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; TNJB; rv:11.0) like Gecko"
    }

    I am curious as to what type this is?  Is it just a string that I can do a 'charfind' on to find the answer to what 'IsMobile' is?  Or is it a typed object?  Or an untyped object with properties that I can reference?

    Thanks.

    Wednesday, July 1, 2015 11:53 AM

Answers

  • Hello,

    Generally speaking with simple JSON one could use a Newton converter coupled with strong typed classes so that you would then be able to iterate the data in a DataTable or List(Of T) but looking at ProfileIds my guess is there could be more or less thus the above would not work.

    For the record here is a link for JSON to classes http://jsonutils.com/

    We can iterate data as shown below yet that really does not provide the ability to use LINQ on it.

    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dynObj As Object = JsonConvert.DeserializeObject(
                <JSON>
    {
      "MatchMethod": "Exact",
      "Difference": 0,
      "DetectionTime": 0.0,
      "Values": {
        "IsMobile": [
          "False"
        ]
      },
      "DataSetName": "Basic",
      "Published": "2015-04-01T00:00:00Z",
      "SignaturesCompared": 0,
      "ProfileIds": {
        "1": 15364,
        "2": 21460,
        "3": 21461,
        "4": 18092
      },
      "Useragent": "Mozilla/5.0 (Windows NT 6.3; WOW64  Trident            rv:11.0",
      "TargetUseragent": "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; TNJB; rv:11.0) like Gecko"
    }                
                </JSON>.Value)
    
            'JContainer is the base class
            Dim jObj = CType(dynObj, JObject)
    
            For Each token As JToken In jObj.Children()
                If TypeOf token Is JProperty Then
                    Dim prop = TryCast(token, JProperty)
                    Console.WriteLine("{0}={1}", prop.Name, prop.Value)
                End If
            Next
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    • Marked as answer by malikmeister Wednesday, July 1, 2015 7:25 PM
    Wednesday, July 1, 2015 1:03 PM

All replies

  • Since 51 degrees is 3rd party you should probably try their support or forum to get assistance with that. There's documentation at the support link also.

    51Degrees support

    Although how did your code return that information? As a string?


    La vida loca


    • Edited by Mr. Monkeyboy Wednesday, July 1, 2015 12:02 PM
    • Proposed as answer by Blackwood Wednesday, July 1, 2015 12:39 PM
    Wednesday, July 1, 2015 12:01 PM
  • Actually,  the way I got the info was:

    dim properties as object
    
     properties = Newtonsoft.Json.Linq.JObject.Parse(json)

    So I don't know what 'properties' is.  I am not too familiar with linq or with json.  Perhaps there are anonymous types of some feature of the language that would explain this.

    Thanks.

     


    Wednesday, July 1, 2015 12:25 PM
  • Hello,

    Generally speaking with simple JSON one could use a Newton converter coupled with strong typed classes so that you would then be able to iterate the data in a DataTable or List(Of T) but looking at ProfileIds my guess is there could be more or less thus the above would not work.

    For the record here is a link for JSON to classes http://jsonutils.com/

    We can iterate data as shown below yet that really does not provide the ability to use LINQ on it.

    Imports Newtonsoft.Json
    Imports Newtonsoft.Json.Linq
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim dynObj As Object = JsonConvert.DeserializeObject(
                <JSON>
    {
      "MatchMethod": "Exact",
      "Difference": 0,
      "DetectionTime": 0.0,
      "Values": {
        "IsMobile": [
          "False"
        ]
      },
      "DataSetName": "Basic",
      "Published": "2015-04-01T00:00:00Z",
      "SignaturesCompared": 0,
      "ProfileIds": {
        "1": 15364,
        "2": 21460,
        "3": 21461,
        "4": 18092
      },
      "Useragent": "Mozilla/5.0 (Windows NT 6.3; WOW64  Trident            rv:11.0",
      "TargetUseragent": "Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; TNJB; rv:11.0) like Gecko"
    }                
                </JSON>.Value)
    
            'JContainer is the base class
            Dim jObj = CType(dynObj, JObject)
    
            For Each token As JToken In jObj.Children()
                If TypeOf token Is JProperty Then
                    Dim prop = TryCast(token, JProperty)
                    Console.WriteLine("{0}={1}", prop.Name, prop.Value)
                End If
            Next
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my webpage under my profile but do not reply to forum questions.

    • Marked as answer by malikmeister Wednesday, July 1, 2015 7:25 PM
    Wednesday, July 1, 2015 1:03 PM