Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following JSON that is stored in a string.
{"authenticationResultCode":"ValidCredentials","brandLogoUri":"http:\/\/dev.virtualearth.net\/Branding\/logo_powered_by.png","copyright":"Copyright © 2013 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.","resourceSets":[{"estimatedTotal":1,"resources":[{"__type":"Location:http:\/\/schemas.microsoft.com\/search\/local\/ws\/rest\/v1","bbox":[51.477271282429321,-0.19857824058491105,51.484996717570674,-0.18203975941508896],"name":"SW6 1HS, London, London, United Kingdom","point":{"type":"Point","coordinates":[51.481134,-0.190309]},"address":{"adminDistrict":"England","adminDistrict2":"London","countryRegion":"United Kingdom","formattedAddress":"SW6 1HS, London, London, United Kingdom","locality":"London","postalCode":"SW6 1HS"},"confidence":"High","entityType":"Postcode1","geocodePoints":[{"type":"Point","coordinates":[51.481134,-0.190309],"calculationMethod":"Rooftop","usageTypes":["Display"]}],"matchCodes":["Good"]}]}],"statusCode":200,"statusDescription":"OK","traceId":"353db0cecdb1401782cb68599d0f7a47|LTSM000185|02.00.183.2300|LTSIPEVM000040, LTSIPEVM000015"}

I have imported JSON.NET and have tried to insert it into a data table as follows:
Dim result As DataTable = DirectCast(JsonConvert.DeserializeObject(returned, (GetType(DataTable))), DataTable)

I sadly get an error message saying:
+		ex	{"Additional text found in JSON string after finishing deserializing object."}	System.Exception

If anyone could help me fix this problem I would be very grateful!
Posted
Comments
ridoy 27-Sep-13 13:19pm    
See http://stackoverflow.com/questions/15811521/json-string-into-datatable-using-vb-net

1 solution

Well, this becomes a philosophical discussion immediately.
I could just answer this by saying, "Oh, escape such and such character and then the it'll probably convert to a table object."
Or I could explain the solution properly.
Remember, JSON is actually name/value pairs. Keep in mind the name is the metadata. It is the thing that describes the data. The data (value) part of the pair is actually what you want to save in the database. Saving the name part of the JSON is akin to saving the columnName in columns named generically like, [Col1], [Col2], etc.

Name/Value Pairs
The point here is that what you really have is an object with some properties (names).
If you create an object with those properties and serialize the JSON into that object then it will be much easier to store the data in a properly created DataTable which has columns named for each of the names in your name/value pairs.

Simplify
So, if you want to get to the solution, first you need to simplify. What should you simplify? Your JSON. So, here's the deal, go ahead and alter your JSON so that it is the following:
{"authenticationResultCode":"ValidCredentials"}


That means, one object with a property name of
authenticationResultCode 
which has a value of:
ValidCredentials


Now, go and see if that one line of code
Dim result As DataTable = DirectCast(JsonConvert.DeserializeObject(returned, (GetType(DataTable))), DataTable)

will even run with that simple JSON.

I'm guessing that it still will not make sense, because what does that do? Does it name the table
authenticationResultCode 
and add a column named
ValidCredentials
? Or does it actually create unnamed table (or default-named table) and then add a column with the name and one row with that value?

Try it and report back. I'm curious to know. I think this simplification will begin to get you there. Let me know.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900