1

I've got this code that retrieves json data from a WebAPI method and then puts it in a string:

const string uri = "http://localhost:48614/api/departments";
var webRequest = (HttpWebRequest)WebRequest.Create(uri);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
{
    var reader = new StreamReader(webResponse.GetResponseStream());
    string s = reader.ReadToEnd();
    var nowIveGotUYouDirtyRat = fastJSON.JSON.Instance.ToObject(s);
    . . . // now what?

...but how do I now use that objectized string to parse the elements of the json array (that it supposedly has assigned to "nowIveGotUYouDirtyRat")?

The documentation seems to take minimalism to an extreme, at least as far as how to accomplish this goes (http://www.codeproject.com/Articles/159450/fastJSON#usingcode).

Also, although I downloaded and compiled fastJSON net35.csproj, the resulting .dll (fastJSON) says it's version is 2.0.0.0 - shouldn't it be 3.5.0.0? (Runtime Version == v2.0.50727)

UPDATE

Alrik, my solution was to switch to JSON.NET. Here's how you can do it with that:

try
{
    const string uri = "http://localhost:28642/api/departments/1/42";
    var webRequest = (HttpWebRequest)WebRequest.Create(uri);
    // GET is the default method/verb, but it's here for clarity
    webRequest.Method = "GET";
    var webResponse = (HttpWebResponse)webRequest.GetResponse();
    if ((webResponse.StatusCode == HttpStatusCode.OK) && (webResponse.ContentLength > 0))
    {
        var reader = new StreamReader(webResponse.GetResponseStream());
        string s = reader.ReadToEnd();
        MessageBox.Show(string.Format("Content from HttpWebRequest is {0}", s));
        var arr = JsonConvert.DeserializeObject<JArray>(s);
        int i = 1;
        foreach (JObject obj in arr)
        {
            var id = (string)obj["Id"];
            var accountId = (double)obj["AccountId"];
            var departmentName = (string)obj["DeptName"];
            //MessageBox.Show(string.Format("Object {0} in JSON array: id == {1}, accountId == {2}, deptName == {3}", i, id, accountId, departmentName));
            i++;
        }
    }
    else
    {
        MessageBox.Show(string.Format("Status code == {0}", webResponse.StatusCode));
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}

Or, if you're just going after a single value:

private void buttonGetDeptCount2_Click(object sender, EventArgs e)
{
    MessageBox.Show(GetScalarVal("http://localhost:28642/api/departments/Count"));
}

private string GetScalarVal(string uri)
{
    var client = new WebClient();
    return client.DownloadString(uri);
}

Note: Kudos to Jon Skeet for the stunningly simple WebClient.DownloadString() tip.

B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    I'm having exactly the same problem. I came from PHP world so it's difficult to understand the concept but in theory the thing is to convert the response to a real object `var newobj = fastJSON.JSON.Instance.ToObject(jsonText);` what I'm missing is the way to use the generated object in all the possible cases for example in an object array or whatever. – Alrik Jan 16 '14 at 07:55
  • 1
    Give newtonsoft/json.net a try. I'll post an update to my question (rather than an answer, since my question is about fastJSON, and the example is with Json.NET). – B. Clay Shannon-B. Crow Raven Jan 16 '14 at 16:05

0 Answers0