JSONの処理2

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
Nect

JSONの処理2

#1

投稿記事 by Nect » 10年前

【知識】
・C言語はポインタや構造体などを利用したプログラムを実装できる
・jansson初心者
・jsonを扱うのがはじめて
・json_t *json_load_file(const char *path, size_t flags, json_error_t *error)やint json_dump_file(const json_t *json, const char *path, size_t flags)などの関数の読み方が全然分からない。書籍でもこのような表記で関数の説明をされると理解できなくなる。

【開発環境】
・C言語
・jansson2.5

【発生している問題】
・Debug Assertion Failed というエラーが発生している
       →調べたところ、メモリ確保を行う前にメモリに書き込んでいるのが原因のようです。c言語の配列であれば、mallocかcallocでメモリを確保すれば解決出来ると思います。今回はjanssonでプログラムを実装しているため、どのように実装すればいいのか分かりません。

・janssonのdocumentの内容が理解できず、プログラムを実装できない

【目的】
・jsonファイルのデータをパースする
・発生しているエラーを解消する
・json_load_file()の引数は/path/to/json.jsonという記述方法で合っているのか確認

【ソースコード】

コード:

 #define _CRT_SECURE_NO_WARNINGS
 #include <stdio.h>
 #include <string.h>
 #include <jansson.h>

 int main(void){
    //json_tはint型ではない
    //json_int_tでint型として利用することができる
    FILE *file;
    json_t *json;
    json_error_t error;

    fopen_s(&file,"json.json","r");

    //  path/to/json.json??
    json=json_load_file("/path/to/json.json",0,&error);

    fclose(file);

    return 0;
}
【エラー画面】
http://kie.nu/1yN9

【参考】
https://jansson.readthedocs.org/en/2.5/apiref.html

アバター
h2so5
副管理人
記事: 2212
登録日時: 13年前
住所: 東京
連絡を取る:

Re: JSONの処理2

#2

投稿記事 by h2so5 » 10年前

json_load_file で内部的にファイルの読み込み処理を行っているので fopen_s や fcloseは不要です。
今回のエラーはfopen_sに失敗している状態でfcloseをしているからだと思います。Janssonやメモリとは関係ありません。

Nect

Re: JSONの処理2

#3

投稿記事 by Nect » 10年前

h2so5 さんが書きました:json_load_file で内部的にファイルの読み込み処理を行っているので fopen_s や fcloseは不要です。
今回のエラーはfopen_sに失敗している状態でfcloseをしているからだと思います。Janssonやメモリとは関係ありません。
迅速な対応ありがとうございます。修正したことでエラーを解消できました。ファイルポインタを利用していないためfcloseも用いる必要がないのですか?

デバッグを開始したら、(null)という結果になってしまいました。json_load_file()がjsonに何も渡せていないということでしょうか?

【ソースコード】

コード:

 #include <stdio.h>
 #include <string.h>
 #include <jansson.h>

 int main(void){

//json_tはint型ではない
//json_int_tでint型として利用することができる
 FILE *file;
 json_t *json;
 json_error_t error;

 json=json_load_file("/path/to/json.json",0,&error);

//
 printf("%s",json);

 return 0;

 }
【実行結果】
(null)

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#4

投稿記事 by みけCAT » 10年前

仕様を見ていませんが、一般的にはファイルの読み込み失敗だと思います。
本当に指定したパスにファイルがあるか確認してください。
相対パスを使用していてカレントディレクトリがどこか自信がない場合、絶対パスを使用してみるのも手です。(問題の切り分け)

ファイルが存在して読み込めるはずである場合、指定しているのファイルの文字コードやJSONの文法をチェックしてみてください。
オフトピック
この場合、%sではなく%pを用いるべきではないのでしょうか?
(仕様を見ていないのでわかりませんが、%sだと不都合が起こる気がします。
最後に編集したユーザー みけCAT on 2013年12月22日(日) 00:32 [ 編集 1 回目 ]
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
h2so5
副管理人
記事: 2212
登録日時: 13年前
住所: 東京
連絡を取る:

Re: JSONの処理2

#5

投稿記事 by h2so5 » 10年前

エラーメッセージを確認しましょう。

https://jansson.readthedocs.org/en/2.5/ ... -reporting

コード:

printf("%s", error.text);
で確認できるはずです。

Nect

Re: JSONの処理2

#6

投稿記事 by Nect » 10年前

みけCAT さんが書きました:仕様を見ていませんが、一般的にはファイルの読み込み失敗だと思います。
本当に指定したパスにファイルがあるか確認してください。
相対パスを使用していてカレントディレクトリがどこか自信がない場合、絶対パスを使用してみるのも手です。(問題の切り分け)

ファイルが存在して読み込めるはずである場合、指定しているのファイルの文字コードやJSONの文法をチェックしてみてください。
オフトピック
この場合、%sではなく%pを用いるべきではないのでしょうか?
(仕様を見ていないのでわかりませんが、%sだと不都合が起こる気がします。
修正しましたが、%pはアドレスを出力するためのものだと認識しています。現在はJSONファイルがパースされているのかを確認したいです。

コード:

json=json_load_file("C:/Users/Runez/Documents/Visual Studio 2012/Projects/json2/json2/json.json",0,&error);
printf("%p",error.text);
【実行結果】
0109FC78

Nect

Re: JSONの処理2

#7

投稿記事 by Nect » 10年前

h2so5 さんが書きました:エラーメッセージを確認しましょう。

https://jansson.readthedocs.org/en/2.5/ ... -reporting

コード:

printf("%s", error.text);
で確認できるはずです。
error.textはjanssonの機能でエラーの状況を確認するためのテキストファイルですか?

【目的】
jsonファイルがパースされていることの確認

コード:

 
json=json_load_file("C:/Users/Runez/Documents/Visual Studio 2012/Projects/json2/json2/json.json",0,&error);

       printf("%s",error.text);
【実行結果】
http://kie.nu/1yO7

アバター
へにっくす
記事: 634
登録日時: 11年前
住所: 東京都

Re: JSONの処理2

#8

投稿記事 by へにっくす » 10年前

Nect さんが書きました:修正しましたが、%pはアドレスを出力するためのものだと認識しています。現在はJSONファイルがパースされているのかを確認したいです。
みけCATさんは、

コード:

printf("%s",json);
でなく

コード:

printf("%p",json);
じゃないのかと聞いてるのですよ(jsonの型はjson_t *で、文字列(char*)じゃないでしょ?)。error.textではありません。まあ、まずはエラーの内容を確認すべきだと思うので、これ以上は言いませんが。
また
Nect さんが書きました:【実行結果】
http://kie.nu/1yO7
を見るに、jsonの解析エラーになっているようです。
'['の指定があるけど、ファイルの最後に達したよ、という感じでしょうか。
jsonの内容を掲示してもらえますか。
written by へにっくす

Nect

Re: JSONの処理2

#9

投稿記事 by Nect » 10年前

へにっくす さんが書きました:
Nect さんが書きました:修正しましたが、%pはアドレスを出力するためのものだと認識しています。現在はJSONファイルがパースされているのかを確認したいです。
みけCATさんは、

コード:

printf("%s",json);
でなく

コード:

printf("%p",json);
じゃないのかと聞いてるのですよ(jsonの型はjson_t *で、文字列(char*)じゃないでしょ?)。error.textではありません。まあ、まずはエラーの内容を確認すべきだと思うので、これ以上は言いませんが。
また
Nect さんが書きました:【実行結果】
http://kie.nu/1yO7
を見るに、jsonの解析エラーになっているようです。
'['の指定があるけど、ファイルの最後に達したよ、という感じでしょうか。
jsonの内容を掲示してもらえますか。
すみません。jsonではなくerror.textだと勘違いしていました。

「jsonの中身を提示してもらえますか?」とはjsonファイルに書き込まれている内容を提示するということでしょうか?
jsonファイルの確認には「JsonViewerPackage」を使用しています。それでjsonファイルを開くと、全角文字列の内容を確認することができません。jsonファイルになる過程で全角文字列がなくなってしまっているのが原因かもしれません。ちゃんと中身を提示したいので、全角も表示できるソフトウェアを探してきます。

コード:

 
 printf("%p",json);
【実行結果】
00000000

アバター
へにっくす
記事: 634
登録日時: 11年前
住所: 東京都

Re: JSONの処理2

#10

投稿記事 by へにっくす » 10年前

JSONファイルはふつうテキストファイルなので、エディタで開いて、その内容を貼り付ければいいのですよ。

JavaScript Object Notation - wikipedia
written by へにっくす

Nect

Re: JSONの処理2

#11

投稿記事 by Nect » 10年前

へにっくす さんが書きました:JSONファイルはふつうテキストファイルなので、エディタで開いて、その内容を貼り付ければいいのですよ。

JavaScript Object Notation - wikipedia
そんな簡単に確認できるとは知りませんでした。どうやらtextファイルでは全角文字列のまま保存できたのですが、jsonファイルで保存すると全角文字列が変化してしまうみたいです。

PHPのstdclassオブジェクトに格納したものをjsonファイルに出力したので、やたら長くなっています。確認よろしくお願いします。

【jsonファイルの中身】
[{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"\u30c6\u30b9\u30c8","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":48,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen \u521d\u30c4\u30a4\u30fc\u30c8\u3000\u3053\u3093\u306a\u7dba\u9e97\u306b\u64ae\u308c\u308b\u3082\u306e\u306a\u3093\u3067\u3059\u306d","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":48,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"\u304d\u308c\u3044\u306a\u98a8\u666f\uff5eHave a Break\uff5e","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}][{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"\u30c6\u30b9\u30c8","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":43,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen \u521d\u30c4\u30a4\u30fc\u30c8\u3000\u3053\u3093\u306a\u7dba\u9e97\u306b\u64ae\u308c\u308b\u3082\u306e\u306a\u3093\u3067\u3059\u306d","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":43,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"\u304d\u308c\u3044\u306a\u98a8\u666f\uff5eHave a Break\uff5e","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}]

アバター
へにっくす
記事: 634
登録日時: 11年前
住所: 東京都

Re: JSONの処理2

#12

投稿記事 by へにっくす » 10年前

真ん中にカンマがありませんよ。また {}の対応が変です。(/* ☆ */のところ)
たぶんそのせいかと思うのですが・・・

ちなみに漢字が\uxxxxに置き換わるのは、JSONのフォーマットに影響されません。
また、トップとして名無しの[][]と配列を2つ持てるのか疑問ですねえ。同じ階層に同じkeyは存在できないはずですから。
(とりあえず//☆以降のを編集して試してみることをお勧めします)

コード:

[{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"\u30c6\u30b9\u30c8","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":48,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen \u521d\u30c4\u30a4\u30fc\u30c8\u3000\u3053\u3093\u306a\u7dba\u9e97\u306b\u64ae\u308c\u308b\u3082\u306e\u306a\u3093\u3067\u3059\u306d","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":48,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"\u304d\u308c\u3044\u306a\u98a8\u666f\uff5eHave a Break\uff5e","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}]/* ☆ここにカンマがありません */[{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"\u30c6\u30b9\u30c8","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":43,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen \u521d\u30c4\u30a4\u30fc\u30c8\u3000\u3053\u3093\u306a\u7dba\u9e97\u306b\u64ae\u308c\u308b\u3082\u306e\u306a\u3093\u3067\u3059\u306d","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":43,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"\u304d\u308c\u3044\u306a\u98a8\u666f\uff5eHave a Break\uff5e","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}]
編集:何だか変だったので元に戻し、おかしいところだけ入れました。
written by へにっくす

Nect

Re: JSONの処理2

#13

投稿記事 by Nect » 10年前

へにっくす さんが書きました:真ん中にカンマがありませんよ。また {}の対応が変です。(/* ☆ */のところ)
たぶんそのせいかと思うのですが・・・

ちなみに漢字が\uxxxxに置き換わるのは、JSONのフォーマットに影響されません。
また、トップとして名無しの[][]と配列を2つ持てるのか疑問ですねえ。同じ階層に同じkeyは存在できないはずですから。
(とりあえず//☆以降のを編集して試してみることをお勧めします)

コード:

[{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"\u30c6\u30b9\u30c8","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":48,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen \u521d\u30c4\u30a4\u30fc\u30c8\u3000\u3053\u3093\u306a\u7dba\u9e97\u306b\u64ae\u308c\u308b\u3082\u306e\u306a\u3093\u3067\u3059\u306d","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":48,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"\u304d\u308c\u3044\u306a\u98a8\u666f\uff5eHave a Break\uff5e","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}]/* ☆ここにカンマがありません */[{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"\u30c6\u30b9\u30c8","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":43,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen \u521d\u30c4\u30a4\u30fc\u30c8\u3000\u3053\u3093\u306a\u7dba\u9e97\u306b\u64ae\u308c\u308b\u3082\u306e\u306a\u3093\u3067\u3059\u306d","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":43,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"\u304d\u308c\u3044\u306a\u98a8\u666f\uff5eHave a Break\uff5e","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}]
編集:何だか変だったので元に戻し、おかしいところだけ入れました。
すみません。JSONの場合の字下げの仕方がわからないので、読みにくいかもしれません。「/* ☆ここにカンマがありません */」を「,」に置き換えればいいのでしょうか?

http://www.ctrlshift.net/jsonprettyprinter/ で対象がパースできる状態になったかどうかを確認しています。これでエラーと表示されなければ、jsonファイルは修正できたということでしょうか?

PHPから取得したJSONオブジェクトを出力したtextファイルと修正したJSONファイルを載せます。

↓修正したjsonファイル

コード:

[{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"\u30c6\u30b9\u30c8","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":48,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen \u521d\u30c4\u30a4\u30fc\u30c8\u3000\u3053\u3093\u306a\u7dba\u9e97\u306b\u64ae\u308c\u308b\u3082\u306e\u306a\u3093\u3067\u3059\u306d","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":48,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"\u304d\u308c\u3044\u306a\u98a8\u666f\uff5eHave a Break\uff5e","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}],[{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"\u30c6\u30b9\u30c8","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":43,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen \u521d\u30c4\u30a4\u30fc\u30c8\u3000\u3053\u3093\u306a\u7dba\u9e97\u306b\u64ae\u308c\u308b\u3082\u306e\u306a\u3093\u3067\u3059\u306d","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitter\u306f\u3058\u3081\u307e\u3057\u305f","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":43,"friends_count":70,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"\u304d\u308c\u3044\u306a\u98a8\u666f\uff5eHave a Break\uff5e","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}]
↓PHPから取得したオブジェクトをテキストファイルに出力したもの

コード:

[{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"テスト","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitterはじめました","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":129,"friends_count":144,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen 初ツイート こんな綺麗に撮れるものなんですね","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitterはじめました","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":129,"friends_count":144,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"きれいな風景~Have a Break~","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}] 

Nect

Re: JSONの処理2

#14

投稿記事 by Nect » 10年前

状況を把握しやすくするために、twitter APIから取得する際に使用したPHPスクリプトを載せておきます。

【環境】
・twitter API1.1
・UltimateOAuth
・PHP 5.4.19

コード:

<?php

 // ライブラリ読み込み
require_once('./UltimateOAuth.php');

 // タイムゾーンを東京に設定
// (date関数をあとで使うならば必要)
//date_default_timezone_set('Asia/Tokyo');

 // パラメータの設定
 $ck = 'ck';
 $cs = 'cs';
 $at = 'at';
 $as = 'as';

 // UltimateOAuthオブジェクト生成
$uo = new UltimateOAuth($ck, $cs, $at, $as);

 // ユーザータイムラインの取得。TwitterからJSON形式の文字列が返ってくるが、
// 自動的にjson_decodeに通されてstdClassオブジェクト化される
$statuses = $uo->get('statuses/user_timeline');

//UTF-8にすることで文字化けを回避できる
header('Content-Type: text/html; charset=utf-8');

//stdClassの処理
//$js = new stdClass;
echo json_encode($statuses,JSON_UNESCAPED_UNICODE);

//ファイルの作成
$filename = './json.json';
$fp=fopen($filename,'w');

//ファイルの書き込み、閉じる
fwrite($fp,sprintf(json_encode($statuses,JSON_UNESCAPED_UNICODE)));

//ファイルの出力
file_get_contents($filename);

fclose($fp);
 
?>

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#15

投稿記事 by みけCAT » 10年前

「修正したjsonファイル」はあなたのプログラムでパースしてエラーは出ましたか?

【追記】提示されたwebサイトではエラーが出ますね。
Error: Extra data: line 1 column 4211 - line 1 column 8423 (char 4211 - 8423)
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#16

投稿記事 by みけCAT » 10年前

このエラーメッセージなどから推測すると、やはりトップレベルの2個目の[~]はinvalidのようですね。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#17

投稿記事 by Nect » 10年前

みけCAT さんが書きました:「修正したjsonファイル」はあなたのプログラムでパースしてエラーは出ましたか?

【追記】提示されたwebサイトではエラーが出ますね。
Error: Extra data: line 1 column 4211 - line 1 column 8423 (char 4211 - 8423)
はい。実行しました。

コード:

 #include <stdio.h>
 #include <string.h>
 #include <jansson.h>

 int main(void){

//json_tはint型ではない
//json_int_tでint型として利用することができる
 json_t *json;
 json_error_t error;

 json=json_load_file("C:/Users/Runez/Documents/Visual Studio 2012/Projects/json2/json2/json.json",0,&error);

 printf("%p",json);

 return 0;

 }
【実行結果】
00000000

アバター
へにっくす
記事: 634
登録日時: 11年前
住所: 東京都

Re: JSONの処理2

#18

投稿記事 by へにっくす » 10年前

Nect さんが書きました:すみません。JSONの場合の字下げの仕方がわからないので、読みにくいかもしれません。「/* ☆ここにカンマがありません */」を「,」に置き換えればいいのでしょうか?
いえ、ここだけではないみたいです。
Nect さんが書きました:http://www.ctrlshift.net/jsonprettyprinter/ で対象がパースできる状態になったかどうかを確認しています。これでエラーと表示されなければ、jsonファイルは修正できたということでしょうか?
はい、その通りです。エラーが表示されないことを確認しましたか?(すでにNo.15、No.16にレスされてるようにエラーになるようですが)
Nect さんが書きました:PHPから取得したJSONオブジェクトを出力したtextファイルと修正したJSONファイルを載せます。
PHPから取得したJSONは大丈夫のようですね。↓

コード:

[
  {
    "created_at":"Sat Nov 16 15:20:41 +0000 2013",
    "id":4.0173155434013e+17,
    "id_str":"401731554340130816",
    "text":"テスト",
    "source":"web",
    "truncated":false,
    "in_reply_to_status_id":null,
    "in_reply_to_status_id_str":null,
    "in_reply_to_user_id":null,
    "in_reply_to_user_id_str":null,
    "in_reply_to_screen_name":null,
    "user":{
      "id":1920164414,
      "id_str":"1920164414",
      "name":"Nect",
      "screen_name":"achivement_w",
      "location":"",
      "description":"twitterはじめました",
      "url":null,
      "entities":{
        "description":{
          "urls":[
          ]
        }
      },
      "protected":false,
      "followers_count":129,
      "friends_count":144,
      "listed_count":0,
      "created_at":"Mon Sep 30 13:53:27 +0000 2013",
      "favourites_count":0,
      "utc_offset":null,
      "time_zone":null,
      "geo_enabled":false,
      "verified":false,
      "statuses_count":2,
      "lang":"ja",
      "contributors_enabled":false,
      "is_translator":false,
      "profile_background_color":"C0DEED",
      "profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
      "profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
      "profile_background_tile":false,
      "profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg",
      "profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg",
      "profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354",
      "profile_link_color":"0084B4",
      "profile_sidebar_border_color":"C0DEED",
      "profile_sidebar_fill_color":"DDEEF6",
      "profile_text_color":"333333",
      "profile_use_background_image":true,
      "default_profile":true,
      "default_profile_image":false,
      "following":false,
      "follow_request_sent":false,
      "notifications":false
    },
    "geo":null,
    "coordinates":null,
    "place":null,
    "contributors":null,
    "retweet_count":0,
    "favorite_count":0,
    "entities":{
      "hashtags":[
      ],
      "symbols":[
      ],
      "urls":[
      ],
      "user_mentions":[
      ]
    },
    "favorited":false,
    "retweeted":false,
    "lang":"ja"
  },
  {
    "created_at":"Tue Oct 01 05:33:43 +0000 2013",
    "id":3.849140005106e+17,
    "id_str":"384914000510599168",
    "text":"@rakuen_rakuen 初ツイート こんな綺麗に撮れるものなんですね",
    "source":"web",
    "truncated":false,
    "in_reply_to_status_id":3.8491019916675e+17,
    "in_reply_to_status_id_str":"384910199166750722",
    "in_reply_to_user_id":869784450,
    "in_reply_to_user_id_str":"869784450",
    "in_reply_to_screen_name":"rakuen_rakuen",
    "user":{
      "id":1920164414,
      "id_str":"1920164414",
      "name":"Nect",
      "screen_name":"achivement_w",
      "location":"",
      "description":"twitterはじめました",
      "url":null,
      "entities":{
        "description":{
          "urls":[
          ]
        }
      },
      "protected":false,
      "followers_count":129,
      "friends_count":144,
      "listed_count":0,
      "created_at":"Mon Sep 30 13:53:27 +0000 2013",
      "favourites_count":0,
      "utc_offset":null,
      "time_zone":null,
      "geo_enabled":false,
      "verified":false,
      "statuses_count":2,
      "lang":"ja",
      "contributors_enabled":false,
      "is_translator":false,
      "profile_background_color":"C0DEED",
      "profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
      "profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png",
      "profile_background_tile":false,
      "profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg",
      "profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg",
      "profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354",
      "profile_link_color":"0084B4",
      "profile_sidebar_border_color":"C0DEED",
      "profile_sidebar_fill_color":"DDEEF6",
      "profile_text_color":"333333",
      "profile_use_background_image":true,
      "default_profile":true,
      "default_profile_image":false,
      "following":false,
      "follow_request_sent":false,
      "notifications":false
    },
    "geo":null,
    "coordinates":null,
    "place":null,
    "contributors":null,
    "retweet_count":0,
    "favorite_count":0,
    "entities":{
      "hashtags":[
      ],
      "symbols":[
      ],
      "urls":[
      ],
      "user_mentions":[
        {
          "screen_name":"rakuen_rakuen",
          "name":"きれいな風景~Have a Break~",
          "id":869784450,
          "id_str":"869784450",
          "indices":[
            0,
            14
          ]
        }
      ]
    },
    "favorited":false,
    "retweeted":false,
    "lang":"ja"
  }
]
そこからなぜ、修正したJSONのようにトップレベルに[]を2つにしたのでしょうか?
4つに増やしたいのなら、

コード:

[
 {},
 {}
]
だったのを

コード:

[
 {},
 {},
 {},
 {}
]
という形にすればいいのに。
written by へにっくす

Nect

Re: JSONの処理2

#19

投稿記事 by Nect » 10年前

へにっくす さんが書きました:
Nect さんが書きました:すみません。JSONの場合の字下げの仕方がわからないので、読みにくいかもしれません。「/* ☆ここにカンマがありません */」を「,」に置き換えればいいのでしょうか?
いえ、ここだけではないみたいです。
Nect さんが書きました:http://www.ctrlshift.net/jsonprettyprinter/ で対象がパースできる状態になったかどうかを確認しています。これでエラーと表示されなければ、jsonファイルは修正できたということでしょうか?
はい、その通りです。エラーが表示されないことを確認しましたか?(すでにレスされてるようにエラーになるようですが)
Nect さんが書きました:PHPから取得したJSONオブジェクトを出力したtextファイルと修正したJSONファイルを載せます。
PHPから取得したJSONは大丈夫のようですね。

そこからなぜ、修正したJSONのようにトップレベルに[]を2つにしたのでしょうか?
4つに増やしたいのなら、

コード:

[
 {},
 {}
]
だったのを

コード:

[
 {},
 {},
 {},
 {}
]
という形にすればいいのに。
すみません。修正した箇所は「/* ☆ここにカンマがありません */」を,に置き換えただけで、他はいじっていません。トップレベルがどこを指しているのかわかりません。

Nect

Re: JSONの処理2

#20

投稿記事 by Nect » 10年前

PHPのスクリプトで再度、jsonファイルを生成したら全角文字列もファイル内に書き込まれていました。先ほど、提示したURLでもパースが成功しました。

↓再度、生成したjsonファイル

コード:

 
[{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"テスト","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitterはじめました","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":130,"friends_count":149,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen 初ツイート こんな綺麗に撮れるものなんですね","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitterはじめました","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":130,"friends_count":149,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"きれいな風景~Have a Break~","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}]

アバター
へにっくす
記事: 634
登録日時: 11年前
住所: 東京都

Re: JSONの処理2

#21

投稿記事 by へにっくす » 10年前

Nect さんが書きました:すみません。修正した箇所は「/* ☆ここにカンマがありません */」を,に置き換えただけで、他はいじっていません。トップレベルがどこを指しているのかわかりません。
他はいじっていない?
PHPから取得したJSONから、No.11に投稿したJSONを作成したわけではないのですね?
No.11をみると、

コード:

[
  {},
  {}
]
[
  {},
  {}
]
というような形にしてるので。ふつうにPHPから取得したJSONをそのまま2回はりつけただけのように見えますが。なのでトップレベルの[]を2つにしたと言いました。
オフトピック
本人はいじったと認識してなくとも、結果としていじってるということはよくあることですよ
(追記:編集してる前に投稿されちゃったので元に戻しました。)
最後に編集したユーザー へにっくす on 2013年12月22日(日) 12:30 [ 編集 2 回目 ]
written by へにっくす

Nect

Re: JSONの処理2

#22

投稿記事 by Nect » 10年前

へにっくす さんが書きました:
Nect さんが書きました:すみません。修正した箇所は「/* ☆ここにカンマがありません */」を,に置き換えただけで、他はいじっていません。トップレベルがどこを指しているのかわかりません。
他はいじっていない?
PHPから取得したJSONから、No.11に投稿したJSONを作成したわけではないのですね?
No.11をみると、

コード:

[
  {},
  {}
]
[
  {},
  {}
]
というような形にしてるので。ふつうにPHPから取得したJSONをそのまま2回はりつけただけのように見えますが。なのでトップレベルの[]を2つにしたと言いました。
オフトピック
本人はいじったと認識してなくとも、結果としていじってるということはよくあることですよ
すみません。プレビューで文章を見直している際、誤って1回分余計にペーストしてしまいました。

アバター
へにっくす
記事: 634
登録日時: 11年前
住所: 東京都

Re: JSONの処理2

#23

投稿記事 by へにっくす » 10年前

Nect さんが書きました:PHPのスクリプトで再度、jsonファイルを生成したら全角文字列もファイル内に書き込まれていました。先ほど、提示したURLでもパースが成功しました。

↓再度、生成したjsonファイル
(省略)
私の方でも確認しました。ちゃんと漢字が出てますね。
ではそのファイルをjanssonで読みましょう。
これで解決ですかね?
written by へにっくす

Nect

Re: JSONの処理2

#24

投稿記事 by Nect » 10年前

へにっくす さんが書きました:
Nect さんが書きました:すみません。修正した箇所は「/* ☆ここにカンマがありません */」を,に置き換えただけで、他はいじっていません。トップレベルがどこを指しているのかわかりません。
他はいじっていない?
PHPから取得したJSONから、No.11に投稿したJSONを作成したわけではないのですね?
No.11をみると、

コード:

[
  {},
  {}
]
[
  {},
  {}
]
というような形にしてるので。ふつうにPHPから取得したJSONをそのまま2回はりつけただけのように見えますが。なのでトップレベルの[]を2つにしたと言いました。
オフトピック
本人はいじったと認識してなくとも、結果としていじってるということはよくあることですよ
Nect さんが書きました:PHPのスクリプトで再度、jsonファイルを生成したら全角文字列もファイル内に書き込まれていました。先ほど、提示したURLでもパースが成功しました。
よかったですね。これで解決ですかね?
提示したURLでパースは成功しました。パースに成功したものの、「パースしたデータを処理するためにJANSSONのソースコード上でパースを行うこと」を目的としています。理想としては配列にjsonファイルの文字列を格納し、格納された配列同士で判定などの処理を行います。

No1で説明不十分でした。目的はjsonファイルのデータを(JANSSONのソースコードで)パースすることです。

Nect

Re: JSONの処理2

#25

投稿記事 by Nect » 10年前

へにっくす さんが書きました:
Nect さんが書きました:PHPのスクリプトで再度、jsonファイルを生成したら全角文字列もファイル内に書き込まれていました。先ほど、提示したURLでもパースが成功しました。

↓再度、生成したjsonファイル
(省略)
私の方でも確認しました。ちゃんと漢字が出てますね。
ではそのファイルをjanssonで読みましょう。
これで解決ですかね?
はい。それで解決です。

Nect

Re: JSONの処理2

#26

投稿記事 by Nect » 10年前

json_t *json_array_get(const json_t *array, size_t index)は配列の要素数(=size_t index)し、それらに代入(const json_t *array)するための関数ですか?size_tはサイズを決定する型ですか?size_tを調べてもわかりませんでした。また、サンプルコードでsize_tの引数が0になっているのですが、メモリーを利用していないということですか?

コード:

root = json_loads(text, 0, &error);
free(text);

if(!root)
{
    fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
    return 1;
}
【サンプルコード】
このページに上記のサンプルコードが記述してあります
https://jansson.readthedocs.org/en/2.5/tutorial.html

Nect

Re: JSONの処理2

#27

投稿記事 by Nect » 10年前

Nect さんが書きました:json_t *json_array_get(const json_t *array, size_t index)は配列の要素数(=size_t index)し、それらに代入(const json_t *array)するための関数ですか?size_tはサイズを決定する型ですか?size_tを調べてもわかりませんでした。また、サンプルコードでsize_tの引数が0になっているのですが、メモリーを利用していないということですか?

コード:

root = json_loads(text, 0, &error);
free(text);

if(!root)
{
    fprintf(stderr, "error: on line %d: %s\n", error.line, error.text);
    return 1;
}
【サンプルコード】
このページに上記のサンプルコードが記述してあります
https://jansson.readthedocs.org/en/2.5/tutorial.html
訂正:json_t *json_array_get(const json_t *array, size_t index)は配列の要素数を決定(=size_t index)し

Nect

Re: JSONの処理2

#28

投稿記事 by Nect » 10年前

実装してみました。関数の引数や型の見方がよくわかりません。そのため、ソースコードでアルゴリズムを実装できず、ソースコードがめちゃくちゃになってしまっています。

以下のアルゴリズムを実装したいです。どなたかアドバイスなどお願いします。

【アルゴリズム】
1.jsonファイルをパース(デコード)する
2.デコードしたjsonファイルのオブジェクトを配列に格納する

【参考】
・jsonのサンプルプログラム
http://mattn.kaoriya.net/software/lang/ ... 214647.htm

・関数の解説
https://jansson.readthedocs.org/en/2.5/ ... html#array

コード:

 #include <stdio.h>
 #include <string.h>
 #include <jansson.h>

typedef struct{

	char com[256];
	char id[256];
}user;

 int main(void){

      /*アルゴリズム
       1.jsonファイルをデコードする
       2.デコードしたjsonファイルのオブジェクトを配列に格納する
      */



        //json_tはint型ではない
       //json_int_tでint型として利用することができる
       FILE *file;
      json_t *json;
      json_error_t error;

      //json.jsonをデコードする
      //json_load_file()はjson_t*型
      json=json_load_file("C:/Users/Runez/Documents/Visual Studio 2012/Projects/json2/json2/json.json",JSON_DECODE_ANY,&error);

      //jsonオブジェクトを扱うための関数??
      json_t *repositories = json_object_get(json, "response");

      //第3引数を第1引数に渡す関数?
      json_array_foreach(, 0,json){

      //jsonオブジェクトのtextの参照
      //オブジェクトを文字配列に変換
     //動作確認のためにprintfを使用
      printf(json_string_value(json_object_get(json,"text"));
}

     return 0;

 }

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#29

投稿記事 by みけCAT » 10年前

とりあえず、39行目の

コード:

printf(json_string_value(json_object_get(json,"text"));
は危険なので、putsを使用する方がいいと思います。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#30

投稿記事 by Nect » 10年前

みけCAT さんが書きました:とりあえず、39行目の

コード:

printf(json_string_value(json_object_get(json,"text"));
は危険なので、putsを使用する方がいいと思います。

コード:

   //動作確認のためにputsを使用
   //printfは外部フォーマット攻撃の危険性がある
   puts(json_string_value(json_object_get(json,"text"));
修正しました。

Nect

Re: JSONの処理2

#31

投稿記事 by Nect » 10年前

またdebug assertion failedというエラーが出てしまいました。今回はfopenとfcloseは使用してません。putsをprintfに変更したらdebug assertion failedは解消されました。

printfに変更し、デバッグとビルドを行い、標準出力を行ったところ(null)(null)という実行結果が返ってきました。json_load_file関数はどのように使えばいいのでしょうか?

コード:

 #include <stdio.h>
 #include <string.h>
 #include <jansson.h>

typedef struct{

	char com[256];
	char id[256];
}user;

 int main(void){

 /*アルゴリズム
 1.jsonファイルをデコードする
 2.デコードしたjsonファイルをオブジェクトに格納する(?)
 3.オブジェクトを配列に格納する
 */



//json_tはint型ではない
//json_int_tでint型として利用することができる
 FILE *file;
 json_t *json;
 json_error_t error;

 //オブジェクトの作成?
 json=json_object();

 //json.jsonをデコードする
 //json_load_file()はjson_t*型
 json=json_load_file("C:/Users/Runez/Documents/Visual Studio 2012/Projects/json2/json2/json.json",JSON_DECODE_ANY,&error);

 puts(json);

 //jsonオブジェクトのtextの参照
 //オブジェクトを文字配列に変換
 //動作確認のためにputsを使用
 //printfは外部フォーマット攻撃の危険性がある
 puts(json_string_value(json_object_get(json,"text")));

 return 0;

 }

Nect

Re: JSONの処理2

#32

投稿記事 by Nect » 10年前

すみません。誤って、解決ボタン押してしまいました。

新たにスレッド立て直してもいいのでしょうか?

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#33

投稿記事 by みけCAT » 10年前

とりあえず、errorを用いてエラーメッセージを表示してみてはいかがですか?
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#34

投稿記事 by Nect » 10年前

お騒がせしました。

副管理人さんありがとうございます。

Nect

Re: JSONの処理2

#35

投稿記事 by Nect » 10年前

みけCAT さんが書きました:とりあえず、errorを用いてエラーメッセージを表示してみてはいかがですか?
No7で発生した問題と同様です。
http://kie.nu/1yO7

コード:

printf("%s",error.text);

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#36

投稿記事 by みけCAT » 10年前

こちらでNo: 20のデータで試したところ、データをUTF-8で保存すればjsonがNULLになりませんでした。

Jansson 2.5を使用しました。
添付ファイル
json_syori.zip
検証に使用したデータ
(17.42 KiB) ダウンロード数: 124 回
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#37

投稿記事 by みけCAT » 10年前

さらに、

コード:

{"text":"text","test":"test","hoge":123}
をjson.jsonに保存して試したと頃、textと表示されました。
元のjsonのトップの[]の中にどうにかして入る必要がありそうです。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#38

投稿記事 by みけCAT » 10年前

トップの[]の中の最初の要素はjson_array_get(json,0)でアクセスできるようです。

コード:

#include <stdio.h>
#include <string.h>
#include <jansson.h>

typedef struct{
	char com[256];
	char id[256];
}user;

int main(void){

	/*アルゴリズム
	1.jsonファイルをデコードする
	2.デコードしたjsonファイルをオブジェクトに格納する(?)
	3.オブジェクトを配列に格納する
	*/



	//json_tはint型ではない
	//json_int_tでint型として利用することができる
	FILE *file;
	json_t *json;
	json_t *json2;
	json_error_t error;

	//オブジェクトの作成?
	json=json_object();

	//json.jsonをデコードする
	//json_load_file()はjson_t*型
	json=json_load_file("json.json",JSON_DECODE_ANY,&error);

	if(json==NULL) {
		puts("json is NULL!");
		puts(error.text);
		return 1;
	}
	// puts(json);

	json2=json_array_get(json,0);

	if(json2==NULL) {
		puts("json2 is NULL!");
		puts(error.text);
		return 1;
	}
	//jsonオブジェクトのtextの参照
	//オブジェクトを文字配列に変換
	//動作確認のためにputsを使用
	//printfは外部フォーマット攻撃の危険性がある
	puts(json_string_value(json_object_get(json2,"text")));

	return 0;

}
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#39

投稿記事 by Nect » 10年前

みけCAT さんが書きました:こちらでNo: 20のデータで試したところ、データをUTF-8で保存すればjsonがNULLになりませんでした。

Jansson 2.5を使用しました。
jsonファイルの中身をUTF-8に変更してから、エンコードするということですか?

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#40

投稿記事 by みけCAT » 10年前

Nect さんが書きました:jsonファイルの中身をUTF-8に変更してから、エンコードするということですか?
意味がわかりません。
この場合の「エンコード」とは何ですか?
「jsonファイルの中身をUTF-8でエンコードする」では無いのですか?
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#41

投稿記事 by Nect » 10年前

みけCAT さんが書きました:
Nect さんが書きました:jsonファイルの中身をUTF-8に変更してから、エンコードするということですか?
意味がわかりません。
この場合の「エンコード」とは何ですか?
「jsonファイルの中身をUTF-8でエンコードする」では無いのですか?
すみません。勘違いしてました。
↓これを行うと思っていました
http://www.xucker.jpn.org/pc/textutf.html

Nect

Re: JSONの処理2

#42

投稿記事 by Nect » 10年前

みけCAT さんが書きました:こちらでNo: 20のデータで試したところ、データをUTF-8で保存すればjsonがNULLになりませんでした。

Jansson 2.5を使用しました。
Readmeに「ここにjansson.h、jansson_config.h、libjansson.aを入れてください。」とあるのですが、libjansson.aがありません。PC内で探しても見つからなかったので、どこかから拾ってきます。

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#43

投稿記事 by みけCAT » 10年前

Nect さんが書きました:
みけCAT さんが書きました:
Nect さんが書きました:jsonファイルの中身をUTF-8に変更してから、エンコードするということですか?
意味がわかりません。
この場合の「エンコード」とは何ですか?
「jsonファイルの中身をUTF-8でエンコードする」では無いのですか?
すみません。勘違いしてました。
↓これを行うと思っていました
http://www.xucker.jpn.org/pc/textutf.html
この操作であっています。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#44

投稿記事 by みけCAT » 10年前

Nect さんが書きました:
みけCAT さんが書きました:こちらでNo: 20のデータで試したところ、データをUTF-8で保存すればjsonがNULLになりませんでした。

Jansson 2.5を使用しました。
Readmeに「ここにjansson.h、jansson_config.h、libjansson.aを入れてください。」とあるのですが、libjansson.aがありません。PC内で探しても見つからなかったので、どこかから拾ってきます。
他の方法でJanssonのヘッダのincludeとライブラリのリンクの設定をできるなら、無くても構いません。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#45

投稿記事 by Nect » 10年前

みけCAT さんが書きました:トップの[]の中の最初の要素はjson_array_get(json,0)でアクセスできるようです。

コード:

#include <stdio.h>
#include <string.h>
#include <jansson.h>

typedef struct{
	char com[256];
	char id[256];
}user;

int main(void){

	/*アルゴリズム
	1.jsonファイルをデコードする
	2.デコードしたjsonファイルをオブジェクトに格納する(?)
	3.オブジェクトを配列に格納する
	*/



	//json_tはint型ではない
	//json_int_tでint型として利用することができる
	FILE *file;
	json_t *json;
	json_t *json2;
	json_error_t error;

	//オブジェクトの作成?
	json=json_object();

	//json.jsonをデコードする
	//json_load_file()はjson_t*型
	json=json_load_file("json.json",JSON_DECODE_ANY,&error);

	if(json==NULL) {
		puts("json is NULL!");
		puts(error.text);
		return 1;
	}
	// puts(json);

	json2=json_array_get(json,0);

	if(json2==NULL) {
		puts("json2 is NULL!");
		puts(error.text);
		return 1;
	}
	//jsonオブジェクトのtextの参照
	//オブジェクトを文字配列に変換
	//動作確認のためにputsを使用
	//printfは外部フォーマット攻撃の危険性がある
	puts(json_string_value(json_object_get(json2,"text")));

	return 0;

}
上記のソースコードでデバッグを開始しました。

http://kie.nu/1yUs というエラーが出てしまいました。

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#46

投稿記事 by みけCAT » 10年前

ライブラリ内部で全角文字が弾かれているかもしれません。
とりあえず、入力を半角文字(半角カタカナを除く)のみにして試してみてください。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#47

投稿記事 by Nect » 10年前

みけCAT さんが書きました:ライブラリ内部で全角文字が弾かれているかもしれません。
とりあえず、入力を半角文字(半角カタカナを除く)のみにして試してみてください。
全角文字を半角文字に置き換えました。エラーが解消されませんでした。全角文字があるとパースできないということでしょうか?

コード:

[{"created_at":"Sat Nov 16 15:20:41 +0000 2013","id":4.0173155434013e+17,"id_str":"401731554340130816","text":"test","source":"web","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitterhajimemasita","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":129,"friends_count":144,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Tue Oct 01 05:33:43 +0000 2013","id":3.849140005106e+17,"id_str":"384914000510599168","text":"@rakuen_rakuen hatutweet","source":"web","truncated":false,"in_reply_to_status_id":3.8491019916675e+17,"in_reply_to_status_id_str":"384910199166750722","in_reply_to_user_id":869784450,"in_reply_to_user_id_str":"869784450","in_reply_to_screen_name":"rakuen_rakuen","user":{"id":1920164414,"id_str":"1920164414","name":"Nect","screen_name":"achivement_w","location":"","description":"twitterhajimeta","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":129,"friends_count":144,"listed_count":0,"created_at":"Mon Sep 30 13:53:27 +0000 2013","favourites_count":0,"utc_offset":null,"time_zone":null,"geo_enabled":false,"verified":false,"statuses_count":2,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000529881117\/1167c67dd8308326170453ec9f7fb403_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1920164414\/1380558354","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[{"screen_name":"rakuen_rakuen","name":"kireina~Have a Break~","id":869784450,"id_str":"869784450","indices":[0,14]}]},"favorited":false,"retweeted":false,"lang":"ja"}]

Nect

Re: JSONの処理2

#48

投稿記事 by Nect » 10年前

json.jsonのパースが成功するとjson_.jsonとしてパース済みのjsonファイルが作成されるのですか?

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#49

投稿記事 by みけCAT » 10年前

3867文字目と3881文字目くらいに全角文字「~」が残っています。
Nect さんが書きました:全角文字があるとパースできないということでしょうか?
こちらの環境では全角文字があっても読み込めているので、どこかで余計なチェックに引っかかっているようです。
なんとか回避できないか探ってみます。
Nect さんが書きました:json.jsonのパースが成功するとjson_.jsonとしてパース済みのjsonファイルが作成されるのですか?
そのような機能は無いはずです。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#50

投稿記事 by Nect » 10年前

みけCAT さんが書きました:3867文字目と3881文字目くらいに全角文字「~」が残っています。
Nect さんが書きました:全角文字があるとパースできないということでしょうか?
こちらの環境では全角文字があっても読み込めているので、どこかで余計なチェックに引っかかっているようです。
なんとか回避できないか探ってみます。
Nect さんが書きました:json.jsonのパースが成功するとjson_.jsonとしてパース済みのjsonファイルが作成されるのですか?
そのような機能は無いはずです。
以下のものをパースすることができませんでした。全角、半角以前の問題のようです。

コード:

[{"first":1,"second":2,"third":3}]

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#51

投稿記事 by みけCAT » 10年前

Nect さんが書きました:以下のものをパースすることができませんでした。全角、半角以前の問題のようです。

コード:

[{"first":1,"second":2,"third":3}]
textが無いからではないでしょうか?
次の入力を試してみてください。

コード:

[{"hoge":"foo","fuga":"WA","text":"Accepted","test":"Wrong Answer"}]
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#52

投稿記事 by Nect » 10年前

みけCAT さんが書きました:
Nect さんが書きました:以下のものをパースすることができませんでした。全角、半角以前の問題のようです。

コード:

[{"first":1,"second":2,"third":3}]
textが無いからではないでしょうか?
次の入力を試してみてください。

コード:

[{"hoge":"foo","fuga":"WA","text":"Accepted","test":"Wrong Answer"}]
デバッグ開始したところ、先ほどと同様のエラーが出てしまいました。エラーの文章にあるassertという単語を見て、思ったのですがもしかして#include<assert.h>が必要なのでしょうか?

http://mattn.kaoriya.net/software/lang/ ... 214647.htm では#include<assert.h>が使われているようです。

Nect

Re: JSONの処理2

#53

投稿記事 by Nect » 10年前

Nect さんが書きました:
みけCAT さんが書きました:
Nect さんが書きました:以下のものをパースすることができませんでした。全角、半角以前の問題のようです。

コード:

[{"first":1,"second":2,"third":3}]
textが無いからではないでしょうか?
次の入力を試してみてください。

コード:

[{"hoge":"foo","fuga":"WA","text":"Accepted","test":"Wrong Answer"}]
デバッグ開始したところ、先ほどと同様のエラーが出てしまいました。エラーの文章にあるassertという単語を見て、思ったのですがもしかして#include<assert.h>が必要なのでしょうか?

http://mattn.kaoriya.net/software/lang/ ... 214647.htm では#include<assert.h>が使われているようです。
#include<assert.h>はassert関数を使うためのライブラリのようなので違うみたいです。

Nect

Re: JSONの処理2

#54

投稿記事 by Nect » 10年前

json_load_fileの引数を修正したら変化がありました。

jsonファイルがあるのに、No such file directoryと表示されることがわかりません。

コード:

#include <stdio.h>
#include <string.h>
#include <jansson.h>

int main(void){
 
    /*アルゴリズム
    1.jsonファイルをデコードする
    2.デコードしたjsonファイルをオブジェクトに格納する(?)
    3.オブジェクトを配列に格納する
    */
 
 
 
    //json_tはint型ではない
    //json_int_tでint型として利用することができる
    json_t *json;
    json_t *json2;
    json_error_t error;
 
    //オブジェクトの作成?
    json=json_object();
 
    //json.jsonをデコードする
    //json_load_file()はjson_t*型
    json=json_load_file("C:Users/Runez/Documents/Visual Studio 2012/Projects/json2/json2/json.json",0,&error);
 
 if(json==NULL) {
        puts("json is NULL!");
        puts(error.text);
        return 1;
 }
    // puts(json);
 
    json2=json_array_get(json,0);
 
    if(json2==NULL) {
        puts("json2 is NULL!");
        puts(error.text);
        return 1;
 }
    //jsonオブジェクトのtextの参照
    //オブジェクトを文字配列に変換
    //動作確認のためにputsを使用
    //printfは外部フォーマット攻撃の危険性がある
    puts(json_string_value(json_object_get(json2,"text")));
 
    return 0;
 
}
【ディレクトリ】
http://kie.nu/1yVL

【実行結果】
http://kie.nu/1yVJ

Nect

Re: JSONの処理2

#55

投稿記事 by Nect » 10年前

変数jsonには代入されていなく、変数json2には代入が行われているということでしょうか?json_load_file関数をどのように修正したらよいのでしょうか?

アドバイスお願いします。

トントン
記事: 100
登録日時: 13年前

Re: JSONの処理2

#56

投稿記事 by トントン » 10年前

過去レスと、ソースは全く見ていませんが。
画像だけで判断すると
拡張子は合っているのでしょうか?

追記
と、思いきやJSONファイルになっているので
問題なさげな感じですね。

Nect

Re: JSONの処理2

#57

投稿記事 by Nect » 10年前

トントン さんが書きました:過去レスと、ソースは全く見ていませんが。
画像だけで判断すると
拡張子は合っているのでしょうか?
jsonを取り扱う拡張子はjsとjsonがあるようです。jsのファイルも生成してありますが、開いて中身を確認することができませんでした。そのため、jsonファイルを使用しています。みけCATさんが同じjsonファイルを使用し、パースに成功しているので、拡張子は間違っていないと思います。

jsonファイルを取り扱うのは今回がはじめてのため、jsとjsonの違いを理解していません。

アバター
へにっくす
記事: 634
登録日時: 11年前
住所: 東京都

Re: JSONの処理2

#58

投稿記事 by へにっくす » 10年前

Nect さんが書きました:

コード:

    //json.jsonをデコードする
    //json_load_file()はjson_t*型
    json=json_load_file("C:Users/Runez/Documents/Visual Studio 2012/Projects/json2/json2/json.json",0,&error);
パス名で'/'スラッシュって使えましたっけ?
まあ使えたとしても、C:/というように:の後に/がないのでルートからでなくなってますけど。
unixなら分かりますが、windowsでは普通'\'バックスラッシュ(円サイン)を使います。
で、C言語の文字列で'\'は特別な意味を持ちますから、2つ続けて記述します。

コード:

"C:\\Users\\Runez\\Documents\\Visual Studio 2012\\Projects\\json2\\json2\\json.json"
とりあえず上記のように変えてみてください。
オフトピック
最初の投稿で型が違うのに平気でprintfに渡そうとしてたり、No.54の投稿でルートからのパスになっていなかったり、基本的なことを理解していない気がしますが大丈夫ですか?
個人的にはC言語の基礎を知らない人にこのJanssonはハードルが高いように思いますけど。
written by へにっくす

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#59

投稿記事 by みけCAT » 10年前

成功するかわかりませんが、jansson.hの

コード:

#define JANSSON_H
という行の下に、

コード:

#define NDEBUG
という行を追加してからJanssonをビルドし、そのライブラリを使ってみてください。
それでダメなら、jansson_private.hの

コード:

#define JANSSON_PRIVATE_H
という行について、同様のことをしてみてください。

【追記】
この方法では上手くいかない可能性が高い気がしました。
帰ったらこちらでビルドしたDLLをアップロードしようと思います。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#60

投稿記事 by Nect » 10年前

へにっくす さんが書きました:
Nect さんが書きました:

コード:

    //json.jsonをデコードする
    //json_load_file()はjson_t*型
    json=json_load_file("C:Users/Runez/Documents/Visual Studio 2012/Projects/json2/json2/json.json",0,&error);
パス名で'/'スラッシュって使えましたっけ?
まあ使えたとしても、C:/というように:の後に/がないのでルートからでなくなってますけど。
unixなら分かりますが、windowsでは普通'\'バックスラッシュ(円サイン)を使います。
で、C言語の文字列で'\'は特別な意味を持ちますから、2つ続けて記述します。

コード:

"C:\\Users\\Runez\\Documents\\Visual Studio 2012\\Projects\\json2\\json2\\json.json"
とりあえず上記のように変えてみてください。
オフトピック
最初の投稿で型が違うのに平気でprintfに渡そうとしてたり、No.54の投稿でルートからのパスになっていなかったり、基本的なことを理解していない気がしますが大丈夫ですか?
個人的にはC言語の基礎を知らない人にこのJanssonはハードルが高いように思いますけど。
修正しました。

現在の力量ではJANSSONでプログラミングをするには早かったようです。自分自身でもまだまだ実力不足だと思っています。

コード:

json=json_load_file("C:\\Users\\Runez\\Documents\\Visual Studio 2012\\Projects\\json2\\json2\\json.json",0,&error);

Nect

Re: JSONの処理2

#61

投稿記事 by Nect » 10年前

みけCAT さんが書きました:成功するかわかりませんが、jansson.hの

コード:

#define JANSSON_H
という行の下に、

コード:

#define NDEBUG
という行を追加してからJanssonをビルドし、そのライブラリを使ってみてください。
それでダメなら、jansson_private.hの

コード:

#define JANSSON_PRIVATE_H
という行について、同様のことをしてみてください。

【追記】
この方法では上手くいかない可能性が高い気がしました。
帰ったらこちらでビルドしたDLLをアップロードしようと思います。
jansson_private.hとjansson.hに追加し、ビルドしました。debug assertion failedというエラーが出てしまいました。

アバター
へにっくす
記事: 634
登録日時: 11年前
住所: 東京都

Re: JSONの処理2

#62

投稿記事 by へにっくす » 10年前

Nect さんが書きました:jansson_private.hとjansson.hに追加し、ビルドしました。debug assertion failedというエラーが出てしまいました。
janssonライブラリのヘッダに手を加えたのですか。
みけCATさんの真意は分かりかねますが、個人的には提供されたライブラリに関するヘッダーには手を付けるべきではないと思います。
手を加えても意味がないと思います。
それで動いたとして、今後janssonライブラリのバージョンが上がった時、どうするのでしょう?同じことするのでしょうか?
本来やりたい事が後回しになってしまいますよ。
追記でうまくいかない可能性が高いと書いてますが、そもそもやってはいけないことです。

もし私が今やっている仕事でそんなことしたら、同じ仲間から総スカン食らいますよ。それくらいのことをしてるのだと思ってください。
元に戻してくださいね。
最後に編集したユーザー へにっくす on 2013年12月23日(月) 21:50 [ 編集 1 回目 ]
written by へにっくす

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#63

投稿記事 by みけCAT » 10年前

こちらでビルドしたDLL(と関連ファイル)です。
これで試してみてください。
添付ファイル
jansson.zip
Jansson 2.5
(87.23 KiB) ダウンロード数: 137 回
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#64

投稿記事 by Nect » 10年前

みけCAT さんが書きました:こちらでビルドしたDLL(と関連ファイル)です。
これで試してみてください。
DLLファイルを使ったことがありません。ソリューションエクスプローラーのヘッダファイルからlibjansson-4.dllを追加すればいいのですか?

Nect

Re: JSONの処理2

#65

投稿記事 by Nect » 10年前

へにっくす さんが書きました:
Nect さんが書きました:jansson_private.hとjansson.hに追加し、ビルドしました。debug assertion failedというエラーが出てしまいました。
janssonライブラリのヘッダに手を加えたのですか。
みけCATさんの真意は分かりかねますが、個人的には提供されたライブラリに関するヘッダーには手を付けるべきではないと思います。
手を加えても意味がないと思います。
それで動いたとして、今後janssonライブラリのバージョンが上がった時、どうするのでしょう?同じことするのでしょうか?
本来やりたい事が後回しになってしまいますよ。
追記でうまくいかない可能性が高いと書いてますが、そもそもやってはいけないことです。

もし私が今やっている仕事でそんなことしたら、同じ仲間から総スカン食らいますよ。それくらいのことをしてるのだと思ってください。
元に戻してくださいね。
予期しない不具合が発生する可能性があるため、ヘッダファイルの編集をしない方がいいということでしょうか?念のために、元の状態に戻しておきました。

Nect

Re: JSONの処理2

#66

投稿記事 by Nect » 10年前

http://visualstudiostudy.blog.fc2.com/blog-entry-6.html
dllの使い方はこれで大丈夫ですか?

元々使っていたjansson.hではなく、jansson.zipに入っているjansson.hを使うということですか?

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#67

投稿記事 by みけCAT » 10年前

まず、DLLからlibファイル(インポートライブラリ)を作成してください。
dllファイルとdefファイルがあるディレクトリで

コード:

LIB /DEF:libjansson-4.dll.def /MACHINE:X86 /out:libjansson-4.dll.lib
というコマンドでできるらしいです。
(出典:http://blogs.konuma.org/blog/2006/11/dlllib_5de9/)

次に、作成したlibファイルを今使用しているプロジェクトにリンクします。
コンパイルし、dllを実行ファイル(exe)と同じディレクトリに置いてから実行します。

ヘッダは両方試してみてください。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
へにっくす
記事: 634
登録日時: 11年前
住所: 東京都

Re: JSONの処理2

#68

投稿記事 by へにっくす » 10年前

Nect さんが書きました:予期しない不具合が発生する可能性があるため、ヘッダファイルの編集をしない方がいいということでしょうか?念のために、元の状態に戻しておきました。
まあ、そういうことです。
みけCATさんがもうdllを提供してるようなので、後はその指示に従ってください。
技術ある人にデバッグ用のを作ってもらうのは仕事でもよくあることですので。
written by へにっくす

Nect

Re: JSONの処理2

#69

投稿記事 by Nect » 10年前

みけCAT さんが書きました:まず、DLLからlibファイル(インポートライブラリ)を作成してください。
dllファイルとdefファイルがあるディレクトリで

コード:

LIB /DEF:libjansson-4.dll.def /MACHINE:X86 /out:libjansson-4.dll.lib
というコマンドでできるらしいです。
(出典:http://blogs.konuma.org/blog/2006/11/dlllib_5de9/)

次に、作成したlibファイルを今使用しているプロジェクトにリンクします。
コンパイルし、dllを実行ファイル(exe)と同じディレクトリに置いてから実行します。

ヘッダは両方試してみてください。
エラーです

【実行結果とディレクトリ】
http://kie.nu/1z12
http://kie.nu/1z14

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#70

投稿記事 by みけCAT » 10年前

libjansson-4.dll.libをlibjansson-4.libにリネームしてからリンクしてみてください。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#71

投稿記事 by Nect » 10年前

みけCAT さんが書きました:libjansson-4.dll.libをlibjansson-4.libにリネームしてからリンクしてみてください。
修正しました。

http://kie.nu/1z1d

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#72

投稿記事 by みけCAT » 10年前

では、libjansson-4.dllをlibjansson-4.dll.dllにリネームしてみてください。
ビルドしなおす必要は無いはずです。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#73

投稿記事 by Nect » 10年前

みけCAT さんが書きました:では、libjansson-4.dllをlibjansson-4.dll.dllにリネームしてみてください。
ビルドしなおす必要は無いはずです。
拡張子を省略して表示する設定になっているため、No69からlibjansson-4.dll.dllのままです。

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#74

投稿記事 by みけCAT » 10年前

libjansson-4.dll.dllをプロジェクトファイルがあるディレクトリにコピーしてみてください。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#75

投稿記事 by Nect » 10年前

みけCAT さんが書きました:libjansson-4.dll.dllをプロジェクトファイルがあるディレクトリにコピーしてみてください。
エラーになってしまいました。同じ手順で、1からやり直してみます。

【ディレクトリ】
http://kie.nu/1z1s
http://kie.nu/1z1q

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#76

投稿記事 by みけCAT » 10年前

No: 69の状態で、json2.exeをエクスプローラから直接実行するとどうなりますか?
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

Nect

Re: JSONの処理2

#77

投稿記事 by Nect » 10年前

みけCAT さんが書きました:No: 69の状態で、json2.exeをエクスプローラから直接実行するとどうなりますか?
動きませんでした

Nect

Re: JSONの処理2

#78

投稿記事 by Nect » 10年前

C1083エラーでjansson.hを読み込まなくなってしまいました。


トントン
記事: 100
登録日時: 13年前

Re: JSONの処理2

#80

投稿記事 by トントン » 10年前

さかのぼって申し訳ないですけど、ふと思ったので。

拡張子を表示させた状態のjsonファイルを見せてもらうことはできますか。
フルパスを見せてもらうことはできますか。(ユーザ名含む)

後、途中うまくいっていたのに突然できなくなったと思うのですがあっていますか。

dllとlibを一から作り直すのも手かもしれません。
設定を一からし直すのも手かもしれません。

Nect

Re: JSONの処理2

#81

投稿記事 by Nect » 10年前

トントン さんが書きました:さかのぼって申し訳ないですけど、ふと思ったので。

拡張子を表示させた状態のjsonファイルを見せてもらうことはできますか。
フルパスを見せてもらうことはできますか。(ユーザ名含む)

後、途中うまくいっていたのに突然できなくなったと思うのですがあっていますか。

dllとlibを一から作り直すのも手かもしれません。
設定を一からし直すのも手かもしれません。
jsonファイルです

http://kie.nu/1z2n

Nect

Re: JSONの処理2

#82

投稿記事 by Nect » 10年前

みけCAT さんが書きました:No: 69の状態で、json2.exeをエクスプローラから直接実行するとどうなりますか?
dllファイルまで作ってもらったのに、ちゃんと動作しないですみません。

Nect

Re: JSONの処理2

#83

投稿記事 by Nect » 10年前

トントン さんが書きました:さかのぼって申し訳ないですけど、ふと思ったので。

拡張子を表示させた状態のjsonファイルを見せてもらうことはできますか。
フルパスを見せてもらうことはできますか。(ユーザ名含む)

後、途中うまくいっていたのに突然できなくなったと思うのですがあっていますか。

dllとlibを一から作り直すのも手かもしれません。
設定を一からし直すのも手かもしれません。
cmakeからやり直しています。

トントン
記事: 100
登録日時: 13年前

Re: JSONの処理2

#84

投稿記事 by トントン » 10年前

Nect さんが書きました:
トントン さんが書きました:さかのぼって申し訳ないですけど、ふと思ったので。

拡張子を表示させた状態のjsonファイルを見せてもらうことはできますか。
フルパスを見せてもらうことはできますか。(ユーザ名含む)

後、途中うまくいっていたのに突然できなくなったと思うのですがあっていますか。

dllとlibを一から作り直すのも手かもしれません。
設定を一からし直すのも手かもしれません。
jsonファイルです

http://kie.nu/1z2n
と思いきや、へにっくすさんとこでパスの指摘があったのでどうでも良かったですね。
流し読み良くないですね。申し訳ないです。

Nect

Re: JSONの処理2

#85

投稿記事 by Nect » 10年前

https://jansson.readthedocs.org/en/late ... arted.html

Jansson can be built with Visual Studio 2010 (and probably newer versions, too). とあります。visual studio2012では出来ないのかもしれません。もし、環境が原因ならパースの手段を変更します。「そこまでしてパースしたいならば、手動でやった方がいい」と言われるかと思います。しかし、膨大なデータを扱うため自動でパースをしたいので、jansson以外の方法で実現させるかもしれません。

協力してくださった方々ありがとうございました。特に、みけCATさんとへにっくすさんどうもありがとうございました。

まだ問題が未解決のため、解決は押しません。このトピックで続行します。

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#86

投稿記事 by みけCAT » 10年前

ビルド設定が64ビット(x64)になっていたら、32ビット(x86)に変更して試してみてください。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#87

投稿記事 by みけCAT » 10年前

「環境を変更」し、gccでJanssonを使用することを提案します。
私が使用しているのはWindows Vista / gcc4.7.2です。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#88

投稿記事 by みけCAT » 10年前

もしくは、Perlを使うのもいいかもしれません。
JSON - search.cpan.org

昔自分が作った時に参考にしたサイトです。
JSON - JSONデータの解析 / Perlモジュール徹底解説 - サンプルコードによるPerl入門
[perl] JSONのエンコード - yref NOTE
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

ISLe
記事: 2650
登録日時: 13年前
連絡を取る:

Re: JSONの処理2

#89

投稿記事 by ISLe » 10年前

Nectさんは、json.jsonファイルをUTF-8で保存するときにメモ帳を使ったようですが、メモ帳はBOMを付加します。
janssonライブラリはBOMをテキストデータとして読み込もうとするそうです。

アバター
みけCAT
記事: 6734
登録日時: 13年前
住所: 千葉県
連絡を取る:

Re: JSONの処理2

#90

投稿記事 by みけCAT » 10年前

ISLe さんが書きました:Nectさんは、json.jsonファイルをUTF-8で保存するときにメモ帳を使ったようですが、メモ帳はBOMを付加します。
janssonライブラリはBOMをテキストデータとして読み込もうとするそうです。
ということは、No: 43で間違ったことを教えてしまったということですね。
申し訳ありませんでした。
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

ISLe
記事: 2650
登録日時: 13年前
連絡を取る:

Re: JSONの処理2

#91

投稿記事 by ISLe » 10年前

janssonライブラリを改造してBOMに対応させるのだとすれば、間違っているとは言えないでしょう。

その前にjson_loads関数を使って周りから固めていったほうが良い気がします。
UTF-8の日本語文字列を取り出せたとしても変換する必要があるのではないでしょうか。

Nect

Re: JSONの処理2

#92

投稿記事 by Nect » 10年前

こんばんは。JSONファイルのパースを再開します。

今更なんですが、PHPでJSONを取得しているからパースもJSONでした方が手っ取り早いような気がします。実装方法わかりませんけど

Nect

Re: JSONの処理2

#93

投稿記事 by Nect » 10年前

以下のアルゴリズムで、JSONファイルから必要なデータだけを抽出するためのプログラムを実装したいです。

【アルゴリズム】
1.JSONファイルのオブジェクトをパースする
2.(必要としている)オブジェクトのみにアクセスする
3.アクセスしたオブジェクトをtextファイルに書き込むなりする

JSONを取得するために使用したスクリプト

コード:

<?php

 // ライブラリ読み込み
require_once('./UltimateOAuth.php');

 // タイムゾーンを東京に設定
// (date関数をあとで使うならば必要)
//date_default_timezone_set('Asia/Tokyo');

 // パラメータの設定
 $ck = 'ck';
 $cs = 'cs';
 $at = 'at';
 $as = 'as';

 // UltimateOAuthオブジェクト生成
$uo = new UltimateOAuth($ck, $cs, $at, $as);

 // ユーザータイムラインの取得。TwitterからJSON形式の文字列が返ってくるが、
// 自動的にjson_decodeに通されてstdClassオブジェクト化される
$statuses = $uo->get('statuses/user_timeline.json?screen_name=sanda1227&count=2');

//UTF-8にすることで文字化けを回避できる
header('Content-Type: text/html; charset=utf-8');

//stdClassの処理
//$js = new stdClass;
echo json_encode($statuses,JSON_UNESCAPED_UNICODE);

//ファイルの作成
//replace関数の使用
$filename = './tweet.json';
$fp=fopen($filename,'w');

//ファイルの書き込み、閉じる
fwrite($fp,sprintf(json_encode($statuses,JSON_UNESCAPED_UNICODE)));

//ファイルの出力
file_get_contents($filename);

fclose($fp);
 
?>

↓twitter APIからHTTPリクエストで取得したオブジェクトをstdclassに格納し、JSONファイルに書き込んだもの

コード:

 
[{"created_at":"Tue Dec 24 16:45:54 +0000 2013","id":4.1552374008083e+17,"id_str":"415523740080828416","text":"最近世の中(主に自分の周りの出来事)があきらかにおかしい。はやく電球を元に戻さなきゃ","source":"<a href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\">Twitter for Android<\/a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":152287295,"id_str":"152287295","name":"いなづき","screen_name":"sanda1227","location":"","description":"意識の高いつぶやきと批評と音楽やデザインのつぶやきをします。将来はフランスにすみたい。","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":18,"friends_count":123,"listed_count":1,"created_at":"Sat Jun 05 15:00:22 +0000 2010","favourites_count":0,"utc_offset":32400,"time_zone":"Irkutsk","geo_enabled":false,"verified":false,"statuses_count":14,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000866797589\/dp2OwBL6_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000866797589\/dp2OwBL6_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/152287295\/1386948029","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":true,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"},{"created_at":"Mon Dec 23 15:30:05 +0000 2013","id":4.1514227092122e+17,"id_str":"415142270921224194","text":"睡眠習慣がしっかりしてる人は天才が多いイメージ","source":"<a href=\"http:\/\/twitter.com\/download\/android\" rel=\"nofollow\">Twitter for Android<\/a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":152287295,"id_str":"152287295","name":"いなづき","screen_name":"sanda1227","location":"","description":"意識の高いつぶやきと批評と音楽やデザインのつぶやきをします。将来はフランスにすみたい。","url":null,"entities":{"description":{"urls":[]}},"protected":false,"followers_count":18,"friends_count":123,"listed_count":1,"created_at":"Sat Jun 05 15:00:22 +0000 2010","favourites_count":0,"utc_offset":32400,"time_zone":"Irkutsk","geo_enabled":false,"verified":false,"statuses_count":14,"lang":"ja","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/378800000866797589\/dp2OwBL6_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/378800000866797589\/dp2OwBL6_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/152287295\/1386948029","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"default_profile":true,"default_profile_image":false,"following":true,"follow_request_sent":false,"notifications":false},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[],"symbols":[],"urls":[],"user_mentions":[]},"favorited":false,"retweeted":false,"lang":"ja"}]
 

Nect

Re: JSONの処理2

#94

投稿記事 by Nect » 10年前

パース失敗しました。echo $obj['text'];の行でエラーが発生しています。どのように修正すればいいでしょうか?

もう1つ質問です。C言語なんでも掲示板では、PHPやPerlなどについての質問は可能ですか?

コード:

<?php

 // ライブラリ読み込み
require_once('./UltimateOAuth.php');

 // タイムゾーンを東京に設定
// (date関数をあとで使うならば必要)
//date_default_timezone_set('Asia/Tokyo');

 // パラメータの設定
 $ck = 'ck';
 $cs = 'cs';
 $at = 'at';
 $as = 'as';

 // UltimateOAuthオブジェクト生成
     $uo = new UltimateOAuth($ck, $cs, $at, $as);

 // ユーザータイムラインの取得。TwitterからJSON形式の文字列が返ってくるが、
// 自動的にjson_decodeに通されてstdClassオブジェクト化される
      $statuses = $uo->get('statuses/user_timeline.json?screen_name=sanda1227&count=2');

//UTF-8にすることで文字化けを回避できる
      header('Content-Type: text/html; charset=utf-8');

//stdClassの処理
//$js = new stdClass;
//これ必要ないかも?
      echo json_encode($statuses,JSON_UNESCAPED_UNICODE);

//ファイルの作成
//replace関数の使用も検討
      $filename = './tweet20.json';
      $fp=fopen($filename,'w');

//ファイルの書き込み
      fwrite($fp,sprintf(json_encode($statuses,JSON_UNESCAPED_UNICODE)));

//ファイルの出力
       file_get_contents($filename);

//読み込み
        $json = file_get_contents("tweet20.json");

//文字化け回避
        $json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');

//エラー発生
         echo $obj['text'];

         fclose($fp);
 
?>

Nect

Re: JSONの処理2

#95

投稿記事 by Nect » 10年前

$objがないことに気づき修正しました。相変わらずエラーです。
Nect さんが書きました:パース失敗しました。echo $obj['text'];の行でエラーが発生しています。どのように修正すればいいでしょうか?

もう1つ質問です。C言語なんでも掲示板では、PHPやPerlなどについての質問は可能ですか?

コード:

<?php

 // ライブラリ読み込み
require_once('./UltimateOAuth.php');

 // タイムゾーンを東京に設定
// (date関数をあとで使うならば必要)
//date_default_timezone_set('Asia/Tokyo');

 // パラメータの設定
 $ck = 'ck';
 $cs = 'cs';
 $at = 'at';
 $as = 'as';

 // UltimateOAuthオブジェクト生成
     $uo = new UltimateOAuth($ck, $cs, $at, $as);

 // ユーザータイムラインの取得。TwitterからJSON形式の文字列が返ってくるが、
// 自動的にjson_decodeに通されてstdClassオブジェクト化される
      $statuses = $uo->get('statuses/user_timeline.json?screen_name=sanda1227&count=2');

//UTF-8にすることで文字化けを回避できる
      header('Content-Type: text/html; charset=utf-8');

//stdClassの処理
//$js = new stdClass;
//これ必要ないかも?
      echo json_encode($statuses,JSON_UNESCAPED_UNICODE);

//ファイルの作成
//replace関数の使用も検討
      $filename = './tweet20.json';
      $fp=fopen($filename,'w');

//ファイルの書き込み
      fwrite($fp,sprintf(json_encode($statuses,JSON_UNESCAPED_UNICODE)));

//ファイルの出力
       file_get_contents($filename);

//読み込み
        $json = file_get_contents("tweet20.json");

//文字化け回避
        $json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');

//エラー発生
         echo $obj['text'];

         fclose($fp);
 
?>

コード:

//文字化け回避
$json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');

$obj = json_decode($json, true);

//エラー発生
echo $obj['text'];

Nect

Re: JSONの処理2

#96

投稿記事 by Nect » 10年前

パース出来ました。皆さん、ご協力ありがとうございました。

閉鎖

“C言語何でも質問掲示板” へ戻る