0
votes

I'm trying to normalize text I got from my website which is using Woocommerce cms. What kind of string is this:

products":[{"title":"INTERMEC PC43T USB Barkod Yaz\u0131c\u0131","id":369,"created_at":"2016-01-24T02:26:13Z","updated_at":"2016-06-13T09:26:04Z","type":"simple","status":"publish","downloadable":false,"virtual":false,"sku":"","price":"239","regular_price":"239","sale_price":null,"price_html":"$239,00</span>","taxable":true,"tax_status":"taxable","tax_class":"","managing_stock":false,"stock_quantity":null,"in_stock":true,"backorders_allowed":false,...

I couldn't pull the values one by one or normalize this string. I've found many things for splitting strings however they didn't work. For example I tried:

Dim product As String = wac.GetAllProducts() ' Get data from WoocommerceApiClient
product.ToArray()
Dim normalized As String() = Split(product, ",", , CompareMethod.Text)
Console.WriteLine(normalized)

... and

Dim normalized As String() = Split(product, """", , CompareMethod.Text)

I got an output something like 0N1GlcZDZCFbQXa5w3GZlCSG3CNbbgxYHZzbNRiZG6k= System.String[]

How can I normalize these strings? I'll also need to get parts (properties and values) of it.

1
That looks rather like JSON, so you should be able to use a tool like JSON.NET to parse it.jmcilhinney
Yes, I used Newtonsoft Json and that solved the problem. Thanks a lot.onurcano

1 Answers

0
votes

I used Newtonsoft Json and that solved the problem, finding this simple json parse method. Here's my final code:

Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq

...

Dim line = wac.GetProducts()

Dim json As String = line.ToString()

Dim ser As JObject = JObject.Parse(json)
Dim data As List(Of JToken) = ser.Children().ToList

For Each item As JProperty In data
    item.CreateReader()
    Select Case item.Name
        ' Case "products"
        '     MessageBox.Show(item.Name)
        Case "products"
            For Each msg As JObject In item.Values

                Dim product_id As String = msg("id")
                Dim product_title As String = msg("title")
                Dim product_price As String = msg("price")
                products_dgv.Rows.Add(product_id, product_title, product_price)


            Next
    End Select
Next