GVKun编程网logo

JSON 之 SuperObject(14): 从 XML 中解析(xml解析json字符格式)

11

在本文中,我们将给您介绍关于JSON之SuperObject(14):从XML中解析的详细内容,并且为您解答xml解析json字符格式的相关问题,此外,我们还将为您提供关于asp.netcore3.0

在本文中,我们将给您介绍关于JSON 之 SuperObject(14): 从 XML 中解析的详细内容,并且为您解答xml解析json字符格式的相关问题,此外,我们还将为您提供关于asp.net core 3.0 JObject The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported、Delphi Superobject,json的通用列表、Delphi7下SuperObject的JSON使用方法、Delphi7怎样用superobject解析Json数据的知识。

本文目录一览:

JSON 之 SuperObject(14): 从 XML 中解析(xml解析json字符格式)

JSON 之 SuperObject(14): 从 XML 中解析(xml解析json字符格式)


SuperObject 文件包中还有一个 SuperXmlParser 单元, 可以从 XML 中解析出 ISuperObject.

SuperXmlParser 只有三个函数: XMLParseString、XMLParseStream、XMLParseFile, 分别从字符串、流、文件中解析.

遗憾的是对中文不够友好.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses SuperObject, SuperXmlParser;

procedure TForm1.Button1Click(Sender: TObject);
var
  jo: ISuperObject;
begin
  jo := XMLParseString(''<Person><Name sex="男">张三</Name><Age>123</Age></Person>'');
  ShowMessage(jo.AsString);

  ShowMessage(jo[''#children''].AsArray[0].AsString);
  ShowMessage(jo[''#children''].AsArray[1].AsString);

  ShowMessage(jo[''#children''].AsArray[0][''#children''].AsArray[0].AsString); {张三}
  ShowMessage(jo[''#children''].AsArray[0][''#attributes.sex''].AsString);      {男}
  ShowMessage(jo[''#children''].AsArray[1][''#children''].AsArray[0].AsString); {123}
end;

//使用压缩
procedure TForm1.Button2Click(Sender: TObject);
var
  jo: ISuperObject;
begin
  jo := XMLParseString(''<Person><Name sex="男">张三</Name><Age>123</Age></Person>'', True);
  ShowMessage(jo.AsString);

  ShowMessage(jo[''Name.Name''].AsString); {张三}
  ShowMessage(jo[''Name.sex''].AsString);  {男}
  ShowMessage(jo[''Age''].AsString);       {123}
end;

end.

 
 
 
 
 

 

 

  
  

asp.net core 3.0 JObject The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported

asp.net core 3.0 JObject The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported

在asp.net core 3.0 中,如果直接在Controller中返回 Jobject 类型,会抛出如下错误:


The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported.
System.NotSupportedException: The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported.
at System.Text.Json.JsonPropertyInfoNotNullable`4.GetDictionaryKeyAndValueFromGenericDictionary(WriteStackFrame& writeStackFrame, String& key, Object& value)
at System.Text.Json.JsonPropertyInfo.GetDictionaryKeyAndValue(WriteStackFrame& writeStackFrame, String& key, Object& value)
at System.Text.Json.JsonSerializer.HandleDictionary(JsonClassInfo elementClassInfo, JsonSerializerOptions options, Utf8JsonWriter writer, WriteStack& state)
at System.Text.Json.JsonSerializer.Write(Utf8JsonWriter writer, Int32 originalWriterDepth, Int32 flushThreshold, JsonSerializerOptions options, WriteStack& state)
at System.Text.Json.JsonSerializer.WriteAsyncCore(Stream utf8Json, Object value, Type inputType, JsonSerializerOptions options, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Formatters.SystemTextJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeFilterPipelineAsync>g__Awaited|19_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Awaited|17_0(ResourceInvoker invoker, Task task, IDisposable scope)
at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)

该问题的出现估计与.net 3.0 新引入的System.Text.Json类库有关.

折衷的解决办法是:

使用Content方法将 JObject 类型的返回值转为 ContentResult 类型.

伪代码如下:


[HttpPost]

public ContentResult Method1([FromBody]Param param1)
{

  JObject result=xxx;

  return Content(result.ToString());

}

如果有时间,建议跟一下System.Text.Json类库的源码来彻底解决该问题.

Delphi Superobject,json的通用列表

Delphi Superobject,json的通用列表

我有一个带有一些TObjectList<> -fields的对象,我尝试使用帮助形式 SuperObject编码为JSON.

TLogs = TObjectList<TLog>;
TMyObject = class(TObject)
private
  FLogs: TLogs;
end;

在SuperObjects代码内部,有一个ToClass过程,迭代字段并将它们添加到json结果中.

在此循环中,检查TRttiFields FieldType.如果它为零,则跳过该对象.

for f in Context.GetType(Value.AsObject.Classtype).GetFields do
  if f.FieldType <> nil then
  begin
    v := f.GetValue(value.AsObject);
    result.AsObject[GetFieldName(f)] := ToJson(v,index);
  end

我的通用列表字段的FieldType为nil.为什么?

如何让SuperObject序列化我的对象列表?

解决方法

这是Delphi的RTTI创建中的一个已知问题.如果你声明你的泛型类,它将无法工作.您需要使用class关键字.

TLogs = class(TObjectList<TLog>);

希望这将在下一个版本中修复.

Delphi7下SuperObject的JSON使用方法

Delphi7下SuperObject的JSON使用方法

总结

以上是小编为你收集整理的Delphi7下SuperObject的JSON使用方法全部内容。

如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。

Delphi7怎样用superobject解析Json数据

Delphi7怎样用superobject解析Json数据

这期内容当中小编将会给大家带来有关Delphi7怎样用superobject解析Json数据,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。

前言

现在不管Webapi还是一般的数据通讯,用的基本都是Json,以前很多的应用程序都是用delphi7开发的,为了维护旧的程序以及与新的接口进行对接(如微信支付宝支付等),我们就用到了superobject来操作Json数据。

由于我们用的是D7,里面没有泛型等这些用法,所以也不存在什么类的序列化与反序列化直接生成Json。操作起来可能比较C#,Android等要麻烦点。

superobject单元

这个可以在网上下载,后面我们会这个unit放到我的网盘上,这里我们用到的是

Delphi7怎样用superobject解析Json数据

Delphi7怎样用superobject解析Json数据

就是上面这个superobject.pas,我的版本是1.25的,原来用了个1.24的里面有几个小BUG(如Currency类型的输入0解析不了),在1.25里面解决了。

基本用法

var
    j, a: ISuperObject;

//不能是TSuperObject,尽管有TSuperObject这个类。
begin
    j:=TSuperObject.Create(stObject);

//创建一个json对象,如果参数是stArray,j就是一个json数组,参数还可以是其它的变量类型,比如字符串,数值之类的,看起来很强大,但是如果参数是一个描述了json的字符串的话,你会得到一个错误。如果想从文本加载,用j:=SO(json文本);

    j.I['数字']:=123; 

//就等于 {"数字": 123} ,类似的,I还可以变成S,B,C之类的,以表示字符串,布尔型,货币型。
    j.S['字符串']:="值";

//就等于 {"字符串": “值”} ,类似的,I还可以变成S,B,C之类的,以表示字符串,布尔型,货币型。

    a:=TSuperObject.Create(stArray);

//创建一个json数组a
    a.I['']:=111; 

//表示给a数组添加一个111元素。注意[]中的值为'',在数组中其[]的语义进行了颠覆性的改变。
    a.I[2]:="s";

//第3个元素值为"s",嗯,[]在superobject的用法还真多
    j['arr']:=a;

//把数组a添加进j,其key名为arr

    showmessage( j.toJson() ); 

//查看j的json文本


    j:=nil; 

//不能写j.Free

end;


代码演示

因为这是在项目的边做边写的,所以我们还是用上一章的《Delphi调用动态链接库》PosPayIntf动态库项目,先看一下我们的目录结构

Delphi7怎样用superobject解析Json数据

我们把Json这块的处理都放在了BaseClass下的Json文件夹下

Delphi7怎样用superobject解析Json数据

Delphi7怎样用superobject解析Json数据

其中superobject就是我们引用的Json解析单元,DoJson是我自己封装的几个处理返回的方法。

Delphi7怎样用superobject解析Json数据

在动态库的项目里面我们可以看到已经把这两个单元文件引用进来了。

DoJson单元

在这个单元里面我写了三个函数

Delphi7怎样用superobject解析Json数据

  • GetJsonStr;获取Json对应的项目下的字符串

  • CreateOutMsgJsonStr;生成输出的OutMsg的Json字符串

  • CreateOutParaJsonStr;生成输出OutParaJsonStr的字符串


GetJsonStr

Delphi7怎样用superobject解析Json数据

根据输入的Json字符串,我们找到指定的属性来获取对应的字符串

如下

Delphi7怎样用superobject解析Json数据

上面传入的这个Json的字符串

我们通过下面方法获取对应的值

Delphi7怎样用superobject解析Json数据

第一个GetJsonStr(Inparastr,'payinfo'),调用这个后我们的tmpParaStr得到的字符串就变成{"oldsaleno":"","payamt":387.6,"oldposno":"","oldpayno":"","salen
o":"201708181158150001","oldsaledate":"","oldrefinfo":""}

第二个GetJsonStr(tmppParaStr,'Payamt'),调用这个后我们的PayTotal值就获取到了387.6,然后我们再把字符串转换为Currency类型即可。


OutParaJsonStr

接口文档

Delphi7怎样用superobject解析Json数据

生成函数

Delphi7怎样用superobject解析Json数据

上在就是一个生成Json字符串的函数

Delphi7怎样用superobject解析Json数据

这个参数后面几个我都有加上默认值,所以在调用的时候如果不用输这些参数就直接给的默认值

调用方法

Delphi7怎样用superobject解析Json数据

返回的Json

Delphi7怎样用superobject解析Json数据


CreateOutMsgJsonStr

这个方法和上面这个基本一样,只不通参数不同

接口文档

Delphi7怎样用superobject解析Json数据

接口函数

Delphi7怎样用superobject解析Json数据

调用方法

Delphi7怎样用superobject解析Json数据

返回的Json

Delphi7怎样用superobject解析Json数据


程序调用后的显示效果

Delphi7怎样用superobject解析Json数据

上述就是小编为大家分享的Delphi7怎样用superobject解析Json数据了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注小编行业资讯频道。

今天的关于JSON 之 SuperObject(14): 从 XML 中解析xml解析json字符格式的分享已经结束,谢谢您的关注,如果想了解更多关于asp.net core 3.0 JObject The collection type ''Newtonsoft.Json.Linq.JObject'' is not supported、Delphi Superobject,json的通用列表、Delphi7下SuperObject的JSON使用方法、Delphi7怎样用superobject解析Json数据的相关知识,请在本站进行查询。

本文标签: