将JSON字符串插入Datatable

编程入门 行业动态 更新时间:2024-10-23 05:41:32
本文介绍了将JSON字符串插入Datatable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下存储在字符串中的JSON。

I have the following JSON that is stored in a string.

{"authenticationResultCode":"ValidCredentials","brandLogoUri":"http:\/\/dev.virtualearth\/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\/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"}

我已导入JSON.NET并拥有尝试将其插入到数据表中,如下所示:

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!

推荐答案

嗯,这立即成为一个哲学讨论。 我c我应该回答这个问题,说:哦,逃避这样的角色,然后它就可能转换成一个桌面物体。 或者我可以正确解释解决方案。 请记住,JSON实际上是名称/值对。请记住,名称是元数据。它是描述数据的东西。该对的数据(值)部分实际上是您要在数据库中保存的内容。保存JSON的名称部分类似于将columnName保存在通常名为[Col1],[Col2]等名称的列中。 名称/价值对 这里的要点是你真正拥有的是一个具有一些属性(名称)的对象。 如果你创建一个具有这些属性的对象将JSON序列化到该对象中然后将数据存储在正确创建的DataTable中会更容易,DataTable具有为您的名称/值对中的每个名称命名的列。 简化 因此,如果您想要解决方案,首先需要简化。你应该简化什么?你的JSON。所以,这是交易,继续改变你的JSON,以便它如下: 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

,其值为:

ValidCredentials

现在,去查看是否有一行代码

Now, go and see if that one line of code

Dim result As DataTable = DirectCast(JsonConvert.DeserializeObject(returned, (GetType(DataTable))), DataTable)

甚至可以使用那个简单的JSON运行。 我猜它仍然没有意义,因为它有什么作用?它是否将表命名为

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

并添加名为

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.

更多推荐

将JSON字符串插入Datatable

本文发布于:2023-07-31 08:40:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1257259.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   JSON   Datatable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!