【发布时间】:2015-03-25 23:44:57
【问题描述】:
首先,我绝对不擅长编程。我一直都是。世界上任何其他人似乎只是“得到”的最简单、最微不足道的事情往往会绊倒我并导致我无法控制的愤怒退出。顺便说一句,对不起这个愚蠢的问题。 :P
我基本上是在尝试获取一些从 HttpWebResponse 返回的 JSON 值并将它们放入 C# 对象中。 JSON 值与客户端列表有关。我希望能够将它们转换为 C# 对象,然后我可以在整个程序中使用这些对象(例如 client.name、client.phone_number 等...)。
下面的代码更像是概念证明而不是最终代码。我登录到一个 WordPress 站点,然后从站点上的一个页面中提取 JSON 数据。我能够检索并查看返回的 JSON 数据(作为一个长而混乱的字符串)。我只是不知道如何从该字符串中提取我需要从中创建“客户端”对象的特定值。
string loginUri = "URL TO WORDPRESS LOGIN";
string username = entryUsername;
string password = entryPassword;
string reqString = "log=" + username + "&pwd=" + password;
byte[] requestData = Encoding.UTF8.GetBytes(reqString);
CookieContainer loginCookie = new CookieContainer();
var request = (HttpWebRequest)WebRequest.Create(loginUri);
request.Proxy = null;
request.AllowAutoRedirect = false;
request.CookieContainer = loginCookie;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = requestData.Length;
using (Stream s = request.GetRequestStream())
{
s.Write(requestData, 0, requestData.Length);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
foreach (Cookie currentCookie in response.Cookies)
{
Console.WriteLine(currentCookie.Name + " = " + currentCookie.Value); // debug purposes
request.CookieContainer.Add(currentCookie);
}
}
}
// Debug purposes
Console.WriteLine("{0} cookie(s) have been downloaded, Jim.", loginCookie.Count.ToString());
string clientListURI = "PAGE CONTAINING JSON";
HttpWebRequest clientListRequest = (HttpWebRequest)WebRequest.Create(clientListURI);
clientListRequest.Proxy = null;
clientListRequest.Method = "POST";
clientListRequest.ContentLength = 0;
clientListRequest.ContentType = "application/json";
clientListRequest.CookieContainer = loginCookie;
using (HttpWebResponse clientListResponse = (HttpWebResponse)clientListRequest.GetResponse())
using (Stream resSteam = clientListResponse.GetResponseStream())
{
JsonValue jsonDoc = JsonObject.Load(resSteam);
Console.WriteLine("Response: {0}", jsonDoc.ToString ());
}
示例 Json:
{
id: "26",
owner: "7",
owner_name: "Bradford Kolumbic",
Status: "0",
status_name: "None",
Milestone: "0",
milestone_name: "None",
small_text_1: "Ghost Buster Solutions",
small_text_8: "Business",
small_text_7: "(858) 123-5432",
small_text_2: "123 ABC Street",
small_text_4: "Escondido",
small_text_5: "California",
small_text_6: "92027"
}
我创建了一个类 Client,可用于存储返回的 JSON 中的每个客户端配置文件。
public class Client
{
public string id { get; set; }
public string owner { get; set; }
public string owner_name { get; set; }
public string Status { get; set; }
public string status_name { get; set; }
public string Milestone { get; set; }
public string milestone_name { get; set; }
public string small_text_1 { get; set; }
public string small_text_8 { get; set; }
public string small_text_7 { get; set; }
public string small_text_2 { get; set; }
public string small_text_4 { get; set; }
public string small_text_5 { get; set; }
public string small_text_6 { get; set; }
}
我尝试了各种不同的方法,但似乎没有什么适合我。我确定我遗漏了一些非常明显的东西。
我尝试的最后一件事是:
var JsonClientList = JsonConvert.DeserializeObject<List<Client>>(jsonDoc);
虽然“无法将 'System.Json.JsonObject' 类型的对象转换为 'System.Json.JsonPrimitive' 类型,但它会出错。”
对这位年轻的傻瓜的任何帮助将不胜感激。感谢您的宝贵时间。
【问题讨论】:
-
检查网站json2csharp.com 将您的 json 粘贴到其中,您将获得所需的课程
-
您能否发布一个您尝试转换的 JSON 示例?
-
@MichalCiechan,确定!以下是来自其中一位客户的示例数据:{ id:“26”,所有者:“7”,所有者名称:“Bradford Kolumbic”,状态:“0”,状态名称:“无”,里程碑:“0”,里程碑名称:“无”,small_text_1:“Ghost Buster 解决方案”,small_text_8:“商业”,small_text_7:“(858) 123-5432”,small_text_2:“ABC 街 123 号”,small_text_4:“Escondido”,small_text_5:“加利福尼亚”,small_text_6: "92027" },
-
您的结果是真正的 JSON 格式,在 JSON 中属性名称使用“”进行转义。例如。 "id":"26"
-
@MichalCiechan 我很抱歉我误解了你,我以为你是在从网站上找一个例子。我收到的实际结果实际上是这样的:“Bradford Kolumbic”、“small_text_8”:“Business”、“small_text_7”:“(858) 123-5432”,-等等...