Skip to content

Instantly share code, notes, and snippets.

@azcoov
Created April 15, 2011 04:31
Show Gist options
  • Save azcoov/921142 to your computer and use it in GitHub Desktop.
Save azcoov/921142 to your computer and use it in GitHub Desktop.
MonoTouch.Dialog Json parsing example
using System;
using System.Collections.Generic;
using System.IO;
using System.Json;
using System.Net;
using MonoTouch.Dialog;
using MonoTouch.Foundation;
using MonoTouch.UIKit;
namespace MonoTouch.Dialog.JsonExample
{
public class Application
{
static void Main (string[] args)
{
UIApplication.Main (args);
}
}
// The name AppDelegate is referenced in the MainWindow.xib file.
public partial class AppDelegate : UIApplicationDelegate
{
RootElement rootElement;
// This method is invoked when the application has loaded its UI and its ready to run
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
window.AddSubview (navigationController.View);
window.MakeKeyAndVisible ();
var root = CreateRoot (GetStackOverflowQuestions ());
var dv = new DialogViewController (root, true);
navigationController.PushViewController (dv, true);
return true;
}
// This method is required in iPhoneOS 3.0
public override void OnActivated (UIApplication application)
{
}
RootElement CreateRoot (List<Question> questions) {
var section = new Section ();
rootElement = new RootElement ("StackOverflow Questions") { section };
foreach (var question in questions) {
var questionRow = new RootElement (question.title);
foreach (var tag in question.tags) {
questionRow.Add( new Section (tag));
}
section.Add (questionRow);
}
return rootElement;
}
//Don't forget to gzip decompress: http://stackapps.com/questions/907/strange-encoding-for-json-output | http://stackoverflow.com/questions/4567313/uncompressing-gzip-response-from-webclient
List<Question> GetStackOverflowQuestions () {
var request = (HttpWebRequest) WebRequest.Create ("http://api.stackoverflow.com/1.1/search?tagged=monotouch");
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (var response = (HttpWebResponse) request.GetResponse ()) {
using (var streamReader = new StreamReader (response.GetResponseStream ())) {
JsonValue root = JsonValue.Load (streamReader);
List<Question> questions = ParseJsonAndLoadQuestions ( (JsonObject) root);
return questions;
}
}
}
public List<Question> ParseJsonAndLoadQuestions (JsonObject json) {
var questions = new List<Question> ();
var questionJson = json["questions"];
foreach (JsonObject jentry in questionJson) {
questions.Add(new Question() {
question_id = jentry["question_id"].ToString(),
tags = ParseAndLoadTags ((JsonArray) jentry["tags"] ),
title = jentry["title"].ToString()
});
}
return questions;
}
public List<string> ParseAndLoadTags (JsonArray json) {
var tags = new List<string> ();
foreach (var tag in json) {
tags.Add (tag);
}
return tags;
}
public class Question {
public string question_id { get; set; }
public string title { get; set; }
public List<string> tags { get; set; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment