gpt4 book ai didi

json - 单点触控 : Deserialize JSON from MVC to Montouch

转载 作者:行者123 更新时间:2023-12-02 10:38:16 25 4
gpt4 key购买 nike

在我的测试 vb.net MVC Web 应用程序中,我有这个 json....

Public Class Person
Public Property Name As String
Public Property Age As Byte
Public Sub New(name As String, age As Byte)
Me.Name = name
Me.Age = age
End Sub
End Class

Function GetPerson() As JsonResult
Dim p As New Person("John Doe", 50)
Return Json(p, JsonRequestBehavior.AllowGet)
End Function

在 Monotouch 中我得到了这个...

JsonObject j;
Uri address = new Uri("http://mysite/home/GetPerson");
HttpWebRequest httpReq = (HttpWebRequest)HttpWebRequest.Create (address);
using (HttpWebResponse httpRes = (HttpWebResponse)httpReq.GetResponse ()) {
Stream s = httpRes.GetResponseStream ();
j = (JsonObject)JsonObject.Load (s);
}

还有这个类...

Public Class Person {
Public string Name { get; set; }
Public byte Age { get; set; }
}

如何将 JsonObject j 解析为 Person 类? ..我希望像 Person p = (Person)j.value;

谢谢!魔力

最佳答案

首先,我会使用 int 来表示 Age。但假设 JSON 结构如下:

{   
"Name" : "John Doe",
"Age" : 100,
}

如果你想使用 System.Json 中烘焙的东西:

var person = new Person()
var obj = JsonObject.Parse(json);

person.Name = obj["Name"].ToString();
person.Age = (int)obj["Age"];

强烈建议使用 ServiceStack.Text不过,它是一个高度优化的极其快速的库,用于使用 JSON,并且与 MonoTouch 和 Mono for Android 兼容......开箱即用!

您可以查看使用 ServiceStack 使用 JSON 的 API here .

关于json - 单点触控 : Deserialize JSON from MVC to Montouch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8450720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com