using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
namespace BLL.Utilities
{
public class JsonHelper
{
public static string SerializeAndFlatten(string prefix, object obj)
{
Dictionary<string, object> dict = new Dictionary<string, object>();
var json = JsonConvert.SerializeObject(obj);
JToken token = JToken.Parse(json);
FillDictionaryFromJToken(dict, token, prefix);
return JsonConvert.SerializeObject(dict);
}
private static void FillDictionaryFromJToken(Dictionary<string, object> dict, JToken token, string prefix)
{
switch (token.Type)
{
case JTokenType.Object:
foreach (JProperty prop in token.Children<JProperty>())
{
FillDictionaryFromJToken(dict, prop.Value, Join(prefix, prop.Name));
}
break;
case JTokenType.Array:
int index = 0;
foreach (JToken value in token.Children())
{
FillDictionaryFromJToken(dict, value, string.Join("", prefix, index));
index++;
}
break;
default:
dict.Add(prefix, ((JValue)token).Value);
break;
}
}
private static string Join(string prefix, string name)
{
return (string.IsNullOrEmpty(prefix) ? name : string.Join("_", prefix, name));
}
}
}
调用
var result = JsonHelper.SerializeAndFlatten(string.Empty, new { Result = QueryStatus.Success.ToString(), ErrorCode = string.Empty, ErrorMessage = string.Empty, CustomerIndicator = indicator, QueryResult = new QueryResult() });
父子结构属性
public class QueryResult
{
[JsonIgnore]
public DateTime CreatedTime { get; set; }
[JsonProperty("reportHeader")]
public virtual ReportHeader ReportHeader { get; set; }
[JsonProperty("personalProfile")]
public virtual PersonalProfile PersonalProfile { get; set; }
[JsonProperty("homeInfo")]
public virtual List<HomeInfo> HomeInfo { get; set; }
[JsonProperty("workInfo")]
public virtual List<WorkInfo> WorkInfo { get; set; }
[JsonProperty("nonRevolvingLoan")]
public virtual NonRevolvingLoan NonRevolvingLoan { get; set; }
[JsonProperty("revolvingLoan")]
public virtual RevolvingLoan RevolvingLoan { get; set; }
[JsonProperty("queryHistory")]
public virtual List<QueryHistory> QueryHistory { get; set; }
}
public class ReportHeader
{
[ForeignKey("BhQueryResult")]
[JsonIgnore]
public long ReportHeaderId { get; set; }
[JsonProperty("reportId")]
[MaxLength(30)]
public string ReportId { get; set; }
[JsonProperty("reportTime")]
public DateTime ReportTime { get; set; }
[JsonProperty("queryResult")]
public int QueryResult { get; set; }
}
public class PersonalProfile
{
[ForeignKey("BhQueryResult")]
[JsonIgnore]
public long PersonalProfileId { get; set; }
[JsonProperty("name")]
[MaxLength(30)]
public string Name { get; set; }
[JsonProperty("pid")]
[MaxLength(20)]
public string Pid { get; set; }
[JsonProperty("mobileCount")]
public int MobileCount { get; set; }
}
public class HomeInfo
{
[JsonIgnore]
public long HomeInfoId { get; set; }
[JsonProperty("homeAddress")]
[MaxLength(200)]
public string HomeAddress { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
}
public class WorkInfo
{
[JsonIgnore]
public long WorkInfoId { get; set; }
[JsonProperty("workName")]
[MaxLength(100)]
public string WorkName { get; set; }
[JsonProperty("workAddress")]
[MaxLength(200)]
public string WorkAddress { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
}
public class NonRevolvingLoan
{
[ForeignKey("BhQueryResult")]
[JsonIgnore]
public long NonRevolvingLoanId { get; set; }
[JsonProperty("summary")]
public virtual NonRevolvingLoanSummary Summary { get; set; }
[JsonProperty("D30")]
public virtual NonRevolvingLoanDetail D30 { get; set; }
[JsonProperty("D90")]
public virtual NonRevolvingLoanDetail D90 { get; set; }
[JsonProperty("D180")]
public virtual NonRevolvingLoanDetail D180 { get; set; }
[JsonProperty("D360")]
public virtual NonRevolvingLoanDetail D360 { get; set; }
}
public class RevolvingLoan
{
[ForeignKey("BhQueryResult")]
[JsonIgnore]
public long RevolvingLoanId { get; set; }
[JsonProperty("summary")]
public virtual RevolvingLoanSummary Summary { get; set; }
[JsonProperty("D30")]
public virtual RevolvingLoanDetail D30 { get; set; }
[JsonProperty("D90")]
public virtual RevolvingLoanDetail D90 { get; set; }
[JsonProperty("D180")]
public virtual RevolvingLoanDetail D180 { get; set; }
[JsonProperty("D360")]
public virtual RevolvingLoanDetail D360 { get; set; }
}
public class QueryHistory
{
[Key]
[JsonIgnore]
public long QueryHistoryId { get; set; }
[JsonProperty("tenantType")]
[MaxLength(30)]
public string TenantType { get; set; }
[JsonProperty("tenantName")]
[MaxLength(50)]
public string TenantName { get; set; }
[JsonProperty("userId")]
[MaxLength(30)]
public string UserId { get; set; }
[JsonProperty("date")]
public DateTime Date { get; set; }
[JsonProperty("reason")]
public int Reason { get; set; }
}
Json data
{
"Data": {
"reportHeader": {
"reportId": "CS1909291422585066440189",
"reportTime": "2019-09-29T14:22:58",
"queryResult": 1
},
"personalProfile": {
"name": "霍测试",
"pid": "111111198988888888",
"mobileCount": 1
},
"homeInfo": [
{
"homeAddress": "联调市测试县**********",
"date": "2019-08-16T00:00:00"
},
{
"homeAddress": "联调市测试县**********",
"date": "2019-08-16T00:00:00"
}
],
"workInfo": [],
"nonRevolvingLoan": {
"summary": {
"loanCount": 1,
"openLoanCount": 1,
"remainingAmount": 53000.0,
"remainingOverdueLoanCount": 0,
"remainingOverdueAmount": 0.0,
"remainingMaxOverdueStatus": "N",
"overdueCount": 0,
"maxOverdueStatus": "N",
"lastCompensationDate": null
},
"D30": {
"applyTenantCount": -1,
"loanCount": 0,
"loanAmount": 0.0,
"loanTenantCount": 0,
"maxLoanAmount": 0.0,
"averageLoanAmount": 0.0,
"overdueLoanCount": 0,
"compensationAmount": 0.0,
"compensationCount": 0.0,
"compensationTimes": 0.0
},
"D90": {
"applyTenantCount": -1,
"loanCount": 1,
"loanAmount": 53000.0,
"loanTenantCount": 1,
"maxLoanAmount": 53000.0,
"averageLoanAmount": 53000.0,
"overdueLoanCount": 0,
"compensationAmount": 0.0,
"compensationCount": 0.0,
"compensationTimes": 0.0
},
"D180": {
"applyTenantCount": -1,
"loanCount": 1,
"loanAmount": 53000.0,
"loanTenantCount": 1,
"maxLoanAmount": 53000.0,
"averageLoanAmount": 53000.0,
"overdueLoanCount": 0,
"compensationAmount": 0.0,
"compensationCount": 0.0,
"compensationTimes": 0.0
},
"D360": {
"applyTenantCount": -1,
"loanCount": 1,
"loanAmount": 53000.0,
"loanTenantCount": 1,
"maxLoanAmount": 53000.0,
"averageLoanAmount": 53000.0,
"overdueLoanCount": 0,
"compensationAmount": 0.0,
"compensationCount": 0.0,
"compensationTimes": 0.0
}
},
"revolvingLoan": {
"summary": {
"accountCount": 0,
"validAccountCount": 0,
"creditLimitSum": 0.0,
"maxCreditLimitPerTenant": 0.0,
"remainingAmount": 0.0,
"remainingOverdueAccountCount": 0,
"remainingOverdueAmount": 0.0,
"remainingMaxOverdueStatus": "N",
"overdueCount": 0,
"maxOverdueStatus": "N",
"revolvingLastCompensationDate": null
},
"D30": {
"applyTenantCount": -1,
"accountCount": 0,
"creditLimitSum": 0.0,
"lendingAmount": 0.0,
"overdueAccountCount": 0,
"revolvingCompensationAmount": 0.0,
"revolvingCompensationTimes": 0.0,
"revolvingCompensationCount": 0.0
},
"D90": {
"applyTenantCount": -1,
"accountCount": 0,
"creditLimitSum": 0.0,
"lendingAmount": 0.0,
"overdueAccountCount": 0,
"revolvingCompensationAmount": 0.0,
"revolvingCompensationTimes": 0.0,
"revolvingCompensationCount": 0.0
},
"D180": {
"applyTenantCount": -1,
"accountCount": 0,
"creditLimitSum": 0.0,
"lendingAmount": 0.0,
"overdueAccountCount": 0,
"revolvingCompensationAmount": 0.0,
"revolvingCompensationTimes": 0.0,
"revolvingCompensationCount": 0.0
},
"D360": {
"applyTenantCount": -1,
"accountCount": 0,
"creditLimitSum": 0.0,
"lendingAmount": 0.0,
"overdueAccountCount": 0,
"revolvingCompensationAmount": 0.0,
"revolvingCompensationTimes": 0.0,
"revolvingCompensationCount": 0.0
}
},
"queryHistory": [
{
"tenantType": "-",
"tenantName": "联调测试(天津)有限公司",
"userId": "DZQCJRZL_TEST_1",
"date": "2019-09-29T00:00:00",
"reason": 1
},
{
"tenantType": "-",
"tenantName": "联调测试(天津)有限公司",
"userId": "DZQCJRZL_TEST_1",
"date": "2019-09-29T00:00:00",
"reason": 1
},
{
"tenantType": "-",
"tenantName": "联调测试(天津)有限公司",
"userId": "DZQCJRZL_TEST_1",
"date": "2019-09-29T00:00:00",
"reason": 1
},
{
"tenantType": "-",
"tenantName": "联调测试(天津)有限公司",
"userId": "DZQCJRZL_TEST_1",
"date": "2019-09-29T00:00:00",
"reason": 1
},
{
"tenantType": "-",
"tenantName": "联调测试(天津)有限公司",
"userId": "DZQCJRZL_TEST_1",
"date": "2019-09-29T00:00:00",
"reason": 1
},
{
"tenantType": "-",
"tenantName": "联调测试(天津)有限公司",
"userId": "DZQCJRZL_TEST_1",
"date": "2019-09-25T00:00:00",
"reason": 6
},
{
"tenantType": "-",
"tenantName": "联调测试(天津)有限公司",
"userId": "DZQCJRZL_TEST_1",
"date": "2019-09-25T00:00:00",
"reason": 1
}
]
},
"StatusCode": "200",
"StatusMessage": "Successful",
"Success": true
}