Main Menu

VB.NET and JSON parser.

Started by QPRJohn, May 26, 2014, 05:35:09 AM

Previous topic - Next topic

QPRJohn

I have started this thread from my previous thread regarding using Exiftool in VB.NET.

Can anyone help me with any examples of using JSON in VB.Net? It's been recommended by Phil Harvey (thanks), I want to be able to fill textboxes on a VB form with the data obtained from Exiftool.

The methods I have produced so far are very slow.

Also, does any one know how to preserve line feeds, I have noticed that line feeds are replaced by full stops .... I can't use VB.Net string replace function as it would put a linefeed at every full stop in the string!

Thanks in advance.

QPRJohn

Update

I have am trying the following code to get the "Description" from a JPG.

Public Class JCXMP2

        Public Property Description() As String
    End Class

    Sub readJSON()
        Dim ofdJPEG As New OpenFileDialog

        If ofdJPEG.ShowDialog() = DialogResult.OK Then

            Dim imagepath As String = ofdJPEG.FileName 'get image file path
            Dim ExifTool As New Process

            ExifTool.StartInfo.FileName = Application.StartupPath & "\Documents\exiftool.exe"
            ExifTool.StartInfo.Arguments = "-L -j -all" + " " + Chr(34) + imagepath + Chr(34)
            ExifTool.StartInfo.UseShellExecute = False
            ExifTool.StartInfo.RedirectStandardOutput = True
            ExifTool.StartInfo.CreateNoWindow = True
            ExifTool.Start()

            Dim retStr = ExifTool.StandardOutput.ReadToEnd()
            frmMetaData.txtCaption.Text = retStr

            ExifTool.Close()

            Dim json As String = retStr
            Dim account As JCXMP2 = JsonConvert.DeserializeObject(Of JCXMP2)(json)

            Debug.Print(account.Description)


            End Sub


It throws an exception with the following error "  Message=Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Quick_Caption.modJSON+JCXMP2' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array."

Can anyone offer some advice?

I'm using VS2010 on Win 7

Phil Harvey

My guess is that your JSON parser expects a single object, but the exiftool -j output returns an array of objects (one object for each input file).  You can either remove the surrounding square brackets and be limited to only one input file, or figure out how to parse an array.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

QPRJohn

Phil

Did as you suggested removed the [] characters using the following code. (I had to remove three characters from end of the string??)
            Dim json As String = retStr
            json = json.Remove(0, 1)
            json = json.Remove(json.Length - 3)
            json = json.Replace("Â", "")


I also had to remove the character "Â" shown before special characters like "©".

Now working thanks (until my next problem!).

Phil Harvey

The 3 characters at the end are likely the bracket, carriage return, and line feed.

The need to remove the funny character likely means that you aren't handling the UTF-8 encoding properly.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

QPRJohn

Can you give me any advice re the UTF-8 encoding?

Phil Harvey

If it is as I suspect, and the exiftool output is valid UTF-8, then I can't help you with the VB.NET side.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Curtis

@QPRJohn,

Not sure if you figured out a fast way to do your JSON tag retrieval, but I just posted here a exiftool .Net wrapper written in vb.net that is very fast.

I updated it a bit to display JSON output as shown in the attached screen shot. 

I was wondering if you really need JSON output or you just need to get the tag values?  For me I have found the -X output (XML format) to be the most useful and complete.  It is also in the Demo program.

If you have any questions about this feel free to contact me direct at curtis1757 at gmail dot com.

Curtis

QPRJohn

Curtis

I've only just seen your post (22/06/14), thanks for the information, I'll look at it and give it a go.

Thanks

Curtis

Great... if you have any questions please feel free to post here of email me.

Curtis