响应 JSON 请求

Response JSON Request

我正在尝试将 JSON .NET 与 WebRequest 一起使用,以使用 "GET" 检索 JSON。本质上,我被困在解析部分并抓住要测试的项目。 WebResponse,我将如何使用 webResponse 检索 JSON 文件? API.php 是我连接到网站数据库进行登录的一种方式。如果登录成功,它returns一个JSON对象。

            string sAddress = "http://hitsparkinteractive.com/api.php";

            // Get the hash
            string addrParams = "action=authenticate";
            addrParams += "&username=" + user;
            addrParams += "&password=" + pwd;

            WebRequest webRequest = WebRequest.Create(sAddress + "?" + addrParams);
            webRequest.Timeout = 3000;
            WebResponse webResponse = webRequest.GetResponse();

            JObject retJSON;
            retJSON = JObject.Parse(webResponse.ToString());

这是使用 WinHTTPRequest 的 Visual Basic 6 的工作代码。

Private Function AuthenticateUser(ByVal index As Long, ByVal Username As String, ByVal Password As String) As Long
     Dim HTTP As WinHttpRequest, sAddress As String, addrParams As String
     Dim JSONParser As Object, retJSON As String, ErrorCode As String, ErrorMsg As String

 On Error Resume Next

 sAddress = "http://hitsparkinteractive.com/api.php"

 addrParams = "action=authenticate"
 addrParams = addrParams & "&username=" & Username
 addrParams = addrParams & "&password=" & Password

 Set HTTP = New WinHttpRequest
 HTTP.Open "GET", sAddress & "?" & addrParams, False
 HTTP.SetTimeouts 250, 250, 250, 3000
 HTTP.Send

 retJSON = HTTP.ResponseText
 Set HTTP = Nothing

 ' Parse your JSON here.
 Set JSONParser = JSON.parse(retJSON) ' What is returned is Scripting.Dictionary object
 If Not JSONParser Is Nothing Then
    If JSONParser.Exists("error") Then  ' keys are case sensitive I believe
        ' We errored out
        ErrorCode = JSONParser.Item("error")

        Select Case ErrorCode
            Case "3"
                AuthenticateUser = 1

            Case Else
                AuthenticateUser = 2
        End Select

        ErrorMsg = JSONParser.Item("message")
        Call AlertMsg(index, ErrorMsg)
        Exit Function
    ElseIf JSONParser.Exists("hash") Then ' we got our hash, sucessfully authenticated
        TempPlayer(index).Hash = JSONParser.Item("hash")
        AuthenticateUser = 0
        Exit Function
    End If
Else
    AuthenticateUser = 3
    Call AlertMsg(index, "Request timed out, please try again.")
    Exit Function
End If
End Function

JSON其实是一个字符串,代表序列化的对象。

ToString returns 对象的字符串表示形式——可能类似于 System.Web.HttpResponse.

您需要的是响应文本,您可以通过 GetResponseStream.

获得

试试这个:

// Get the hash
string addrParams = string.Format("action=authenticate&username={0}&password={1}", user, pwd;)
string url = string.Concat(sAddress, "?", addrParams);

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);
webRequest.Timeout = 3000;

using (HttpWebResponse response = webRequest.GetResponse() as HttpWebResponse)
{
    if (response.StatusCode == HttpStatusCode.OK) {

        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(MyTypeJsonResponse));
        object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());

        //json parsing response
        MyTypeJsonResponse response = objResponse as Response;
    }
}

[DataContract]
public class MyTypeJsonResponse
{
    [DataMember(Name = "myProperty2")]
    public string MyProperty1 { get; set; }

    [DataMember(Name = "myProperty2")]
    public string MyProperty2 { get; set; }
}