草庐IT

newtonsoft.json.jsonserializationException已被抛出-Xamarin

程序员大本营 2024-06-04 原文

我正在使用我的应用程序进行实时提要。转换为JSON并尝试在我的应用程序上显示的是,这是一个例外。我已经查看了与此问题有关的许多答案,但是大多数答案与下面的代码中的答案相同吗?如何解决这个问题?

活动

{    string result = new HTTPDataHandler().GetHTTPData(@params[0]);    return result;}protected override void OnPostExecute(string result){    RssObject data = JsonConvert.DeserializeObject<RssObject>(result);    mDialog.Dismiss();    DataAdapter adapter = new DataAdapter(data, mainActivity);    mainActivity.rv.SetAdapter(adapter);    adapter.NotifyDataSetChanged();}

模型

public class Feed{    public string url { get; set; }    public string title { get; set; }    public string link { get; set; }    public string author { get; set; }    public string description { get; set; }    public string image { get; set; }}public class Item{    public string title { get; set; }    public string pubDate { get; set; }    public string link { get; set; }    public string guid { get; set; }    public string author { get; set; }    public string thumbnail { get; set; }    public string description { get; set; }    public string content { get; set; }    public List<object> enclosure { get; set; }    public List<string> categories { get; set; }}public class RssObject{    public string status { get; set; }    public Feed feed { get; set; }    public List<Item> items { get; set; }}

错误

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.Object]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object.Path 'items[0].enclosure.link', line 1, position 1142.

JSON

"status": "ok""feed": {"url": "http://rss.nytimes.com/services/xml/rss/nyt/Technology.xml""title": "NYT &gt; Technology""link": "https://www.nytimes.com/section/technology?partner=rss&amp;emc=rss""author": """description": """image": "https://static01.nyt.com/images/misc/NYT_logo_rss_250x40.png"}"items": [{"title": "Uber Fires Executive Over Handling of Rape Investigation in India""pubDate": "2017-06-08 00:55:39""link": "https://www.nytimes.com/2017/06/07/technology/uber-fires-executive.html?partner=rss&amp;emc=rss""guid": "https://www.nytimes.com/2017/06/07/technology/uber-fires-executive.html""author": "MIKE ISAAC""thumbnail": """description": "The president of Uber’s Asia business was terminated after reporters questioned why he obtained medical records of a woman attacked by her Uber driver.""content": "The president of Uber’s Asia business was terminated after reporters questioned why he obtained medical records of a woman attacked by her Uber driver.""enclosure": {"link": "https://static01.nyt.com/images/2017/06/08/business/08UBER1-sub/08UBER1-sub-moth.jpg"}"categories": ["0": "Uber Technologies Inc""1": "Sex Crimes""2": "Ethics and Official Misconduct""3": "Alexander, Eric"]}

看答案

如果我正确理解,您正在尝试使用转换为JSON的RSS提要进行工作。据我所知,在XML中,机箱标签具有三个必需的属性“ URL”,“长度”和“ type”。因此,第一个问题是围栏的形式不佳。

假设JSON是您想要的。在JSON中,外壳是一个对象,请注意 {} 不是数组 []。但是在c#中宣布为 List<object> 通常代表JSON中的数组。因此,您正在尝试对 object 进入 List<object>.

有几种方法可以解决此问题。第一,如果您知道外壳的架构,请创建一个新类以表示模式并将其用作类型(或列表,如果应该是数组)而不是 List<object>。第二,如果不需要封闭,只需删除C#类中的属性,因此在避难所中可以忽略它。如果您需要外壳,但不知道该模式可能是什么,请告诉我们,我们将花更多的周期为您找到解决方案。

有关newtonsoft.json.jsonserializationException已被抛出-Xamarin的更多相关文章

  1. ruby-on-rails - Rails HTML 请求渲染 JSON - 2

    在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这

  2. ruby - 在 Ruby 中重新分配常量时抛出异常? - 2

    我早就知道Ruby中的“常量”(即大写的变量名)不是真正常量。与其他编程语言一样,对对象的引用是唯一存储在变量/常量中的东西。(侧边栏:Ruby确实具有“卡住”引用对象不被修改的功能,据我所知,许多其他语言都没有提供这种功能。)所以这是我的问题:当您将一个值重新分配给常量时,您会收到如下警告:>>FOO='bar'=>"bar">>FOO='baz'(irb):2:warning:alreadyinitializedconstantFOO=>"baz"有没有办法强制Ruby抛出异常而不是打印警告?很难弄清楚为什么有时会发生重新分配。 最佳答案

  3. ruby-on-rails - 如何使用 Rack 接收 JSON 对象 - 2

    我有一个非常简单的RubyRack服务器,例如:app=Proc.newdo|env|req=Rack::Request.new(env).paramspreq.inspect[200,{'Content-Type'=>'text/plain'},['Somebody']]endRack::Handler::Thin.run(app,:Port=>4001,:threaded=>true)每当我使用JSON对象向服务器发送POSTHTTP请求时:{"session":{"accountId":String,"callId":String,"from":Object,"headers":

  4. ruby - 用 YAML.load 解析 json 安全吗? - 2

    我正在使用ruby2.1.0我有一个json文件。例如:test.json{"item":[{"apple":1},{"banana":2}]}用YAML.load加载这个文件安全吗?YAML.load(File.read('test.json'))我正在尝试加载一个json或yaml格式的文件。 最佳答案 YAML可以加载JSONYAML.load('{"something":"test","other":4}')=>{"something"=>"test","other"=>4}JSON将无法加载YAML。JSON.load("

  5. ruby - 如何以编程方式检查证书是否已被吊销? - 2

    我正在开发一个xcode自动构建系统。在执行一些预构建验证时,我想检查指定的证书文件是否已被撤销。我了解securityverify-cert验证其他证书属性但不验证吊销。我如何检查撤销?我正在用Ruby编写构建系统,但我对任何语言的想法都持开放态度。我阅读了这个答案(Openssl-Howtocheckifacertificateisrevokedornot),但指向底部的链接(DoesOpenSSLautomaticallyhandleCRLs(CertificateRevocationLists)now?)进入的Material对我的目的来说有点过于复杂(用户上传已撤销的证书是一

  6. ruby-on-rails - Rails - Carrierwave 进程抛出 ArgumentError : no images in this image list - 2

    在尝试实现应用auto_orient的过程之后!对于我的图片,我收到此错误:ArgumentError(noimagesinthisimagelist):app/uploaders/image_uploader.rb:36:in`fix_exif_rotation'app/controllers/posts_controller.rb:12:in`create'Carrierwave在没有进程的情况下工作正常,但在添加进程后尝试上传图像时抛出错误。流程如下:process:fix_exif_rotationdeffix_exif_rotationmanipulate!do|image|

  7. ruby-on-rails - Rails 渲染带有驼峰命名法的 json 对象 - 2

    我在一个简单的RailsAPI中有以下Controller代码:classApi::V1::AccountsControllerehead:not_foundendendend问题在于,生成的json具有以下格式:{id:2,name:'Simpleaccount',cash_flows:[{id:1,amount:34.3,description:'simpledescription'},{id:2,amount:1.12,description:'otherdescription'}]}我需要我生成的json是camelCase('cashFlows'而不是'cash_flows'

  8. ruby - 使用 JSON gem 将自定义对象转换为 JSON - 2

    我正在学习如何使用JSONgem解析和生成JSON。我可以轻松地创建数据哈希并将其生成为JSON;但是,在获取一个类的实例(例如Person实例)并将其所有实例变量放入哈希中以转换为JSON时,我脑袋放屁。这是我遇到问题的例子:require"json"classPersondefinitialize(name,age,address)@name=name@age=age@address=addressenddefto_jsonendendp=Person.new('JohnDoe',46,"123ElmStreet")p.to_json我想创建一个.to_json方法,这样我就可以获

  9. ruby-on-rails - 如何使用驼峰键名称从 Rails 返回 JSON - 2

    我正在构建一个带有Rails后端的JS应用程序,为了不混淆snake和camelcases,我想通过从服务器返回camelcase键名来规范化这一切。因此,当从API返回时,user.last_name将返回user.lastName。我如何实现这一点?谢谢!编辑:添加Controller代码classApi::V1::UsersController 最佳答案 我的方法是使用ActiveModelSerializer和json_api适配器:在你的Gemfile中,添加:gem'active_model_serializers'创建

  10. ruby-on-rails - 如何将数组输出为 JSON? - 2

    我有以下内容:@array.inspect["x1","x2","adad"]我希望能够将其格式化为:client.send_message(s,m,{:id=>"x1",:id=>"x2",:id=>"adad"})client.send_message(s,m,???????)如何在????????中获得@array输出?空间作为ID?谢谢 最佳答案 {:id=>"x1",:id=>"x2",:id=>"adad"}不是有效的散列,因为您有键冲突它应该是这样的:{"ids":["x1","x2","x3"]}更新:@a=["x1

随机推荐