Link to home
Start Free TrialLog in
Avatar of john hill
john hill

asked on

json string

Hello Experts

I am getting a json response from a URL which I am trying parse it and capture code to store in the DB

Its returning the string ok but I am unable to remove the quoted strings to save the code in the DB
 

 Dim jResults As Object = JObject.Parse(Json)
        Dim jsonResulttodict = JsonConvert.DeserializeObject(Of Dictionary(Of String, Object))(Json)
        Dim firstItem = jsonResulttodict.Item("error")
        Dim seconditem = jsonResulttodict.Item("success")
        Dim thirditem = jsonResulttodict.Item("codes")

example string:

'{"error":"","success":"1 code generated successfully","codes":["QTESTA2HLT"]}


I am getting the code as [{  "QTESTA2HLT"  }]

Al I want is to read as QTESTA2HLT

please advise

thanks
Avatar of HainKurt
HainKurt
Flag of Canada image

use:

mydata = mydata.replace("""","")

Open in new window


to get rid of "s
maybe you should use

Dim thirditem = jsonResulttodict.Item("codes")(0)

Open in new window


or

thirditem = thirditem.replace("[{  ""","").replace("""  }]","")

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Fernando Soto
Fernando Soto
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I agree with Fernando, the issue is that codes is an array.  Please accept above for your case but for others who were looking to remove quotes in text value, the escape character is a slash; therefore, it would be .replaceAll("\"", "");
@Kevin

the escape character is a slash; therefore, it would be .replaceAll("\"", "");

this is vb.net, so double " in string is a single "

and no need to convert to string!
if you look at authors code, he is getting the correct value with

Dim thirditem = jsonResulttodict.Item("codes")

Open in new window


so, as I mentioned in my post above, ID: 42305599, both should work:

Dim thirditem = jsonResulttodict.Item("codes")(0)

Open in new window


or, string replace

thirditem = thirditem.replace("[{  ""","").replace("""  }]","")

Open in new window

Ah, too sleepy.  I thought it was JavaScript.  Saw the JSON and didn't look at the question zones.  Point still stands that the issue is the array versus bad quotes in the string.  The solution is to use the index of first element versus parsing the string representation of the array, so you are correct that both your second code above and Fernando's post should work. I vote YES.
Avatar of john hill
john hill

ASKER

thank you so much for your input
Not a problem John, glad to help.