Cannot access child value on Newtonsoft.Json.Linq.JValue
开发项目框架为.net framework,遇到此问题原因是笔者在做接口转发时接口返回类型直接定义为了object类型,这导致格式化返回结果时出现如标题异常,具体代码如下
try
{
var result = XXXWepApi.WebApiPost<请求参数类, object>("api/xxx接口地址", new 请求参数类());
if (result != null)
{
if (result.Code == "200")
return SuccessResult(result.Data);
else
return ErrorResult("列表获取失败!", 10001);
}
else
return ErrorResult("列表为空!", 10002);
}
catch (Exception ex)
{
Function.WriteErrorLog(ex);
return ErrorResult("系统繁忙,请重新获取!", 10050);
}
解决代码如下:转换抛出异常是因为自定义格式化方法格式化返回SuccessResult(result.Data)时抛出错误,result.Data返回时一个字符串数组,定义一个返回类即可皆解决,代码如下
try
{
var result = XXXWepApi.WebApiPost<请求参数类, 【定义返回类】>("api/xxx接口地址", new 请求参数类());
if (result != null)
{
if (result.Code == "200")
return SuccessResult(result.Data);
else
return ErrorResult("列表获取失败!", 10001);
}
else
return ErrorResult("列表为空!", 10002);
}
catch (Exception ex)
{
Function.WriteErrorLog(ex);
return ErrorResult("系统繁忙,请重新获取!", 10050);
}
关于SuccessResult方法如下
/// <summary>
/// 返回成功信息
/// </summary>
/// <param name="data">附加内容</param>
/// <param name="msg">成功信息</param>
/// <returns>返回Result格式信息</returns>
[NonAction]
public HttpResponseMessage SuccessResult(object data = null, string msg = "成功")
{
return new HttpResponseMessage
{
Content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(new ResultV2<object>()
{
Code = 200,
Msg = msg,
Data = data == null ? new { } : data
}), System.Text.Encoding.UTF8, "application/json")
};
}
Cannot access child value on Newtonsoft.Json.Linq.JValue的更多相关文章
- Can not add Newtonsoft.Json.Linq.JValue to Newtonsoft.Json.Linq.JObject.
https://blog.csdn.net/zhouyingge1104/article/details/83307637 C#项目中使用NewtonSoft.json,报错提示: Can not a ...
- Newtonsoft.Json.Linq对象读取DataSet数据
Newtonsoft.Json.Linq对象读取DataSet数据: private void button4_Click(object sender, EventArgs e) { ...
- Newtonsoft.Json.Linq
var json = "{\"name\":\"ok1\",\"sex\":\"man\"}"; / ...
- 遍历Newtonsoft.Json.Linq.JObject
JObject 遍历: 引用命名空间:using Newtonsoft.Json.Linq; JObject _jObject = JObject.Parse("{'ID':'001','M ...
- Newtonsoft.Json.Linq 常用方法总结
目录 1.Entity to Json 1.1.准备工作 1.2.Entity to Json 1.3.Json to Entity 2.Linq To Json 2.1.创建对象 2.2.从 Jso ...
- Newtonsoft.Json.Linq.JObject 遍历验证每个属性内容
业务需求,拦截器验证每个请求inputstream(实际是application/json流)的数据,但是json反序列化实体格式不同. var req = filterContext.Request ...
- 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 ...
- Newtonsoft.Json.Linq 序列化 反序列化等知识
1.反序列化实体类 //使用JObject读写字符串:JObject j = JObject.Parse(data);IEnumerable<JProperty> properties = ...
- Newtonsoft.json中 linq to json 和序列化哪个快?
Newtonsoft.json是最常用的json序列化组件,当然他不是最快的,但是是功能最全的.. using System; using System.Collections.Generic; us ...
- Newtonsoft.Json C# Json序列化和反序列化工具的使用、类型方法大全 C# 算法题系列(二) 各位相加、整数反转、回文数、罗马数字转整数 C# 算法题系列(一) 两数之和、无重复字符的最长子串 DateTime Tips c#发送邮件,可发送多个附件 MVC图片上传详解
Newtonsoft.Json C# Json序列化和反序列化工具的使用.类型方法大全 Newtonsoft.Json Newtonsoft.Json 是.Net平台操作Json的工具,他的介绍就 ...
随机推荐
- 周立功DTU+温度传感器,ZWS物联网平台尝试
1.前言 了解到周立功有相关的物联网云平台,近期在调研动态环境监控项目,可以进行一个上云的尝试.购置了传感器.周立功的DTU等硬件,将传感器的温度.湿度等数据进行一个云平台的上传. 2.前期准备 传感 ...
- table 动态隐藏tr行
table: <table style="width:100%" class="table01" cellspacing="1" ce ...
- C++编程笔记(GPU并行编程-2)
C++与CUDA 内存管理 封装 利用标准库容器实现对GPU的内存管理 #include <iostream> #include <cuda_runtime.h> #inclu ...
- 《Java口袋指南》-内容总结
Java口袋指南 一.语言 1.命名 类名:大驼峰 泛型:E标识集合元素 方法名:小驼峰 变量名:小写 包名:小写或下划线 2.词法元素/token 字符串压缩优化 ...
- 【小项目】微信定时推送天气预报Github项目使用及原理介绍-包含cron、天气预报、常用api
一.资料链接 1.github地址 https://github.com/qq1534774766/wx-push 2.教程地址 https://blog.csdn.net/qq15347747/ar ...
- STM32外部中断(EXTI)控制LED亮灭的代码
led.c #include "led.h" void LED_Config(void) { GPIO_InitTypeDef GPIO_InitStruct; RCC_APB2P ...
- 正则提取器和beanshell处理器组合,将提取的所有id拼接成字符串
1.添加正则表达式,提取所有id值 2.添加beanshell处理器将所有的id值拼接成字符串 方法一: int N = Integer.parseInt(vars.get("build_m ...
- go语言的切片特性
概述: 在使用切片进行赋值的时候,产生新的数组的指针指向原来的数组,只要修改新数组中的元素时,原来数组的元素也会改变. 测试代码: func TestSliceShareMemory(t *testi ...
- 【转载】EXCEL VBA 自动筛选—AutoFilter方法
AutoFilter方法的语法及说明 下面是Range对象的AutoFilter方法的语法: Range对象.AutoFilter(Field,Criterial1,Operator,C ...
- 学习.NET MAUI Blazor(三)、创建.NET MAUI Blazor应用并使用AntDesignBlazor
大致了解了Blazor和MAUI之后,尝试创建一个.NET MAUI Blazor应用. 需要注意的是: 虽然都叫MAUI,但.NET MAUI与.NET MAUI Blazor 并不相同,MAUI还 ...