C# 在C中反序列化3taps JSON数据#
这是我第一次使用JSON。我在调试时得到一个“null”解析响应。不确定是反序列化JSON错误,还是直接传输对象错误。请帮忙 JSON如下所示:C# 在C中反序列化3taps JSON数据#,c#,.net,json,api,C#,.net,Json,Api,这是我第一次使用JSON。我在调试时得到一个“null”解析响应。不确定是反序列化JSON错误,还是直接传输对象错误。请帮忙 JSON如下所示: { "success": true, "anchor": 1257781939, "postings": [ { "id": 1257767757, "external_url": "http://miami.craigslist.org/brw/atq/4591006794.html", "he
{
"success": true,
"anchor": 1257781939,
"postings": [
{
"id": 1257767757,
"external_url": "http://miami.craigslist.org/brw/atq/4591006794.html",
"heading": "Antique Chairs Two ",
"body": "\n Antique Chairs Two of them, the seat is 18\" hi $95 for two. call anytime\n show contact info\n ",
"timestamp": 1406731621,
"price": 95,
"images": [
{
"full": "http://images.craigslist.org/00404_bc7wiVq6I7O_600x450.jpg"
}
],
"annotations": {
"source_continent": "USA",
"source_cat": "sss",
"source_neighborhood": "coconut creek",
"source_state": "Florida",
"source_loc": "miami",
"source_subloc": "brw",
"source_map_google": "https://maps.google.com/maps/preview/@26.253500,-80.177500,16z",
"source_map_yahoo": "http://maps.yahoo.com/#mvt=m&lat=26.253500&lon=-80.177500&zoom=16",
"latlong_source": "In Posting",
"proxy_ip": "186.91.140.57:8080",
"source_heading": "Antique Chairs Two ",
"phone": "9545543144",
"source_account": "dmpsk-4591006794@sale.craigslist.org",
"original_posting_date": "1406553713",
"source_subcat": "ata|atq",
"condition": "new"
},
"location": {
"country": "USA",
"state": "USA-FL",
"metro": "USA-MIA",
"region": "USA-MIA-BRO",
"county": "USA-FL-BRW",
"city": "USA-MIA-POM",
"zipcode": "USA-33066",
"lat": "26.2535",
"long": "-80.1775",
"accuracy": 8,
"geolocation_status": 3
}
}
using System;
using System.Collections.Generic;
namespace AutoPoster.Core.DTO
{
public class PostResponse
{
public bool success {get; set;}
public uint anchor {get; set;}
public List<Postings> Postings {get; set;}
}
}
已更新
我的DTO文件夹中有四个类,分别名为PostResponse、Postings、Annotations和Location。第一个PostResponse如下所示:
{
"success": true,
"anchor": 1257781939,
"postings": [
{
"id": 1257767757,
"external_url": "http://miami.craigslist.org/brw/atq/4591006794.html",
"heading": "Antique Chairs Two ",
"body": "\n Antique Chairs Two of them, the seat is 18\" hi $95 for two. call anytime\n show contact info\n ",
"timestamp": 1406731621,
"price": 95,
"images": [
{
"full": "http://images.craigslist.org/00404_bc7wiVq6I7O_600x450.jpg"
}
],
"annotations": {
"source_continent": "USA",
"source_cat": "sss",
"source_neighborhood": "coconut creek",
"source_state": "Florida",
"source_loc": "miami",
"source_subloc": "brw",
"source_map_google": "https://maps.google.com/maps/preview/@26.253500,-80.177500,16z",
"source_map_yahoo": "http://maps.yahoo.com/#mvt=m&lat=26.253500&lon=-80.177500&zoom=16",
"latlong_source": "In Posting",
"proxy_ip": "186.91.140.57:8080",
"source_heading": "Antique Chairs Two ",
"phone": "9545543144",
"source_account": "dmpsk-4591006794@sale.craigslist.org",
"original_posting_date": "1406553713",
"source_subcat": "ata|atq",
"condition": "new"
},
"location": {
"country": "USA",
"state": "USA-FL",
"metro": "USA-MIA",
"region": "USA-MIA-BRO",
"county": "USA-FL-BRW",
"city": "USA-MIA-POM",
"zipcode": "USA-33066",
"lat": "26.2535",
"long": "-80.1775",
"accuracy": 8,
"geolocation_status": 3
}
}
using System;
using System.Collections.Generic;
namespace AutoPoster.Core.DTO
{
public class PostResponse
{
public bool success {get; set;}
public uint anchor {get; set;}
public List<Postings> Postings {get; set;}
}
}
使用系统;
使用System.Collections.Generic;
命名空间AutoPoster.Core.DTO
{
公开课回复
{
公共bool成功{get;set;}
公共uint锚点{get;set;}
公共列表发布{get;set;}
}
}
JSON数据处理得很好,但是当反序列化时,我得到parsedResponse=null。这是我将JSON映射到模型对象的调用
if (string.IsNullOrEmpty(jsonResponse))
return postInfo;
var parsedResponse = await Task.Run(() => JsonConvert.DeserializeObject<DTO.Postings>(jsonResponse)).ConfigureAwait(false);
postInfo = await MapDtoToPostingInfo(parsedResponse).ConfigureAwait(false);
return postInfo;
if(string.IsNullOrEmpty(jsonResponse))
返回postInfo;
var parsedResponse=await Task.Run(()=>JsonConvert.DeserializeObject(jsonResponse)).ConfigureAwait(false);
postInfo=await MapDtoToPostingInfo(解析响应)。ConfigureAwait(false);
返回postInfo;
任何帮助都将不胜感激。谢谢 问题在于,
Postings
类只表示返回的响应对象的一部分(即,Postings
数组中的单个对象),而不是根
尝试反序列化表示整个响应对象的类。例如:
public class Response
{
public bool success {get; set;}
public uint anchor {get; set;}
public List<Postings> Postings {get; set;}
}
公共类响应
{
公共bool成功{get;set;}
公共uint锚点{get;set;}
公共列表发布{get;set;}
}
当您使用JSON时,我的建议是使用它来生成正确的类
public class Image
{
public string full { get; set; }
}
public class Annotations
{
public string source_continent { get; set; }
public string source_cat { get; set; }
public string source_neighborhood { get; set; }
public string source_state { get; set; }
public string source_loc { get; set; }
public string source_subloc { get; set; }
public string source_map_google { get; set; }
public string source_map_yahoo { get; set; }
public string latlong_source { get; set; }
public string proxy_ip { get; set; }
public string source_heading { get; set; }
public string phone { get; set; }
public string source_account { get; set; }
public string original_posting_date { get; set; }
public string source_subcat { get; set; }
public string condition { get; set; }
}
public class Location
{
public string country { get; set; }
public string state { get; set; }
public string metro { get; set; }
public string region { get; set; }
public string county { get; set; }
public string city { get; set; }
public string zipcode { get; set; }
public string lat { get; set; }
public string @long { get; set; }
public int accuracy { get; set; }
public int geolocation_status { get; set; }
}
public class Posting
{
public int id { get; set; }
public string external_url { get; set; }
public string heading { get; set; }
public string body { get; set; }
public int timestamp { get; set; }
public int price { get; set; }
public List<Image> images { get; set; }
public Annotations annotations { get; set; }
public Location location { get; set; }
}
public class RootObject
{
public bool success { get; set; }
public int anchor { get; set; }
public List<Posting> postings { get; set; }
}
公共类映像
{
公共字符串已满{get;set;}
}
公共类注释
{
公共字符串源{get;set;}
公共字符串源_cat{get;set;}
公共字符串源_邻域{get;set;}
公共字符串源_状态{get;set;}
公共字符串源_loc{get;set;}
公共字符串源_子脚本{get;set;}
公共字符串源\映射\谷歌{get;set;}
公共字符串源\映射\雅虎{get;set;}
公共字符串latlong_源{get;set;}
公共字符串代理_ip{get;set;}
公共字符串源_标题{get;set;}
公用字符串电话{get;set;}
公共字符串源_帐户{get;set;}
公共字符串原始发布日期{get;set;}
公共字符串源_子类{get;set;}
公共字符串条件{get;set;}
}
公共类位置
{
公共字符串国家{get;set;}
公共字符串状态{get;set;}
公共字符串metro{get;set;}
公共字符串区域{get;set;}
公共字符串country{get;set;}
公共字符串city{get;set;}
公共字符串zipcode{get;set;}
公共字符串lat{get;set;}
公共字符串@long{get;set;}
公共整数精度{get;set;}
公共int地理位置_状态{get;set;}
}
公开课张贴
{
公共int id{get;set;}
公共字符串外部_url{get;set;}
公共字符串标题{get;set;}
公共字符串体{get;set;}
公共int时间戳{get;set;}
公共整数价格{get;set;}
公共列表图像{get;set;}
公共批注{get;set;}
公共位置位置{get;set;}
}
公共类根对象
{
公共bool成功{get;set;}
公共int锚点{get;set;}
公共列表发布{get;set;}
}
然后,您应该能够反序列化到RootObject中
var rootObject = JsonConvert.DeserializeObject<RootObject>(jsonResponse);
var rootObject=JsonConvert.DeserializeObject(jsonResponse);
这种反应似乎不是很好。我找不到\posting
的右括号(]
),也找不到右括号。我假设它只是被截断了?您的Postings
类表示响应的posting数组中的一个对象。您正在尝试反序列化您的响应对象,以。。。您需要定义一个类来表示响应对象。是的,它被截断了。很抱歉。这只是众多帖子之一。不想全部发布。此外,如果粘贴的是jsonResponse,则需要一个包含列表
和成功
以及锚定
属性的类。我创建了一个响应类,但它仍然没有反序列化。:/谢谢你提醒我注意。让我很快尝试一下。谢谢