ブラウザでダウンロード
サンプルに添付した json.txt は、Twitter の verify_credentials.json
で取得したものです。
Json.NET からダウンロードしたアセンブリをソースコードと同じ場所に置いて
__libPath.txt に既に記述済みの該当アセンブリの先頭のセミコロンを削除し
て下さい。Framework2.0 か Framework3.5 の選択ですが、両方インストール
されている場合はどちらでもいいと思います。
※ Framework は __buildPath.txt で選択して下さい
関連する記事
PHP + Twitter API : 資格情報の確認と アプリケーションに割り当てられた My Access Token
' ********************************************************
' ■ アセンブリのダウンロード
' http://json.codeplex.com/
' ■ マニュアル
' http://james.newtonking.com/projects/json/help/
' ********************************************************
Imports System.IO
Imports System.Text
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Module MyModule
' ********************************************************
' FileStream は Stream を継承
' ********************************************************
Sub Main()
' WEB(Twitter) から返された json フォーマットのテキストを開く
Dim JsonText As StreamReader = New StreamReader( "json.txt", Encoding.UTF8 )
' 全て読み込む
Dim Text As String = JsonText.ReadToEnd()
' 逆シリアライズ
Dim JsonObject As Object = JsonConvert.DeserializeObject( Text )
' 閉じる
JsonText.Close()
' Object として参照
Console.WriteLine( JsonObject("statuses_count") )
' プロパティで一覧参照
Dim jp As JProperty
For Each jp In JsonObject
' jp.Value は JToken
Console.WriteLine( jp.Name + " : " + jp.Value.ToString() )
Next
End Sub
End Module
以下は実行結果です
>json.exe
968
statuses_count : 968
notifications : false
profile_sidebar_border_color : "F2E195"
description : "絵を描くプログラマ"
location : ""
screen_name : "sworc"
profile_use_background_image : true
followers_count : 13
status : {
"favorited": false,
"coordinates": null,
"source": "<a href=\"http://winofsql.jp/\" rel=\"nofollow\">TwitLink</a>",
"created_at": "Sun Jun 27 07:07:03 +0000 2010",
"place": null,
"in_reply_to_screen_name": null,
"truncated": false,
"in_reply_to_user_id": null,
"id": 17148268393,
"contributors": null,
"geo": null,
"in_reply_to_status_id": null,
"text": "資料 TCPDF 利用前の注意事項 http://bit.ly/cXWWH2"
}
contributors_enabled : false
friends_count : 4
lang : "ja"
geo_enabled : false
profile_background_color : "000000"
favourites_count : 0
verified : false
profile_text_color : "0C3E53"
following : false
time_zone : "Osaka"
created_at : "Sat Nov 21 04:24:25 +0000 2009"
profile_link_color : "FF0000"
protected : false
profile_background_image_url : "http://a3.twimg.com/profile_background_images/59645045/bbs_img_4598c0b36c78d.jpg"
name : "night walker"
profile_sidebar_fill_color : "FFF7CC"
url : "http://winofsql.jp/tegaki"
profile_image_url : "http://a3.twimg.com/profile_images/536588405/tw_normal.png"
id : 91500526
profile_background_tile : false
utc_offset : 32400
以下は、二通りの参照方法です
( どちらも同じ結果になります )
※ 値がダブルクォートで挟まれている場合は、.Value が使用でき、
※ ダブルクォートが省かれます( ダブルクォートが無いとエラー )
' Object として参照
Console.WriteLine( JsonObject("statuses_count") )
Console.WriteLine()
Console.WriteLine( JsonObject("status") )
Console.WriteLine()
Console.WriteLine( JsonObject("status")("source").Value )
Console.WriteLine( "----------------------------------------------" )
' パスで取得
Console.WriteLine( JsonObject.SelectToken("statuses_count") )
Console.WriteLine()
Console.WriteLine( JsonObject.SelectToken("status") )
Console.WriteLine()
Console.WriteLine( JsonObject.SelectToken("status.source").Value )
Console.WriteLine( "----------------------------------------------" )
Object の場合、JProperty で下位オブジェクトを取得し、
JObject の場合は、キー/値ペア で下位オブジェクトを取得します。
( JObject.Parse を使用すると、JObject として明示して取得できます )
' Object => JProperty
Dim jp As JProperty
For Each jp In JsonObject
Console.WriteLine( jp.Name + " : " + jp.Value.ToString() )
Next
' JObject => キー/値ペア
Dim kv As System.Collections.Generic.KeyValuePair(Of String, JToken)'
For Each kv In Ctype(JsonObject,JObject)
Console.WriteLine( kv.Key + " : " + kv.Value.ToString() )
Next
以下は配列のサンプルです。
( [ ] は配列です )
--------------------------------------------------
【実行】
--------------------------------------------------
Console.WriteLine( JsonObject.SelectToken("test[0].k_name").Value )
Console.WriteLine( JsonObject.SelectToken("test[0].k_value").Value )
Console.WriteLine( JsonObject.SelectToken("test[1].k_name").Value )
Console.WriteLine( JsonObject.SelectToken("test[1].k_value").Value )
--------------------------------------------------
【データ】
--------------------------------------------------
test : [
{
"k_name": "A",
"k_value": "123"
},
{
"k_name": "B",
"k_value": "456"
}
]
--------------------------------------------------
【結果】
--------------------------------------------------
A
123
B
456