0

こんにちは、以下は私のJSONファイルです

{
    "completed_in": 0.046,
    "max_id": 286437550577573889,
    "max_id_str": "286437550577573889",
    "next_page": "?page=2&max_id=286437550577573889&q=new%20year",
    "page": 1,
    "query": "new+year",
    "refresh_url": "?since_id=286437550577573889&q=new%20year",
    "results": [{
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "RodiSilverstone",
        "from_user_id": 855001483,
        "from_user_id_str": "855001483",
        "from_user_name": "Rodi Silverstone",
        "geo": null,
        "id": 286437550577573889,
        "id_str": "286437550577573889",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2978713010\/5e321e174ded0c14f11d4929c1928912_normal.png",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2978713010\/5e321e174ded0c14f11d4929c1928912_normal.png",
        "source": "<a href="http:\/\/twitter.com\/">web<\/a>",
        "text": "Bonne annee\/joyeux Noel en retard :S\nHappy New Year\/Merry Christmas w\/ late...\nPS: Pas d'accents car clav' Qwerty car I'm in Australia ^^",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "ToddBuell",
        "from_user_id": 461294085,
        "from_user_id_str": "461294085",
        "from_user_name": "Todd Buell",
        "geo": null,
        "id": 286437550334291970,
        "id_str": "286437550334291970",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2482121747\/bqf3z7knjm3zetmdkc7g_normal.jpeg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2482121747\/bqf3z7knjm3zetmdkc7g_normal.jpeg",
        "source": "<a href="http:\/\/twitter.com\/">web<\/a>",
        "text": "Fiscal cliff deal gives markets relief in New Year. \"Super Mario\" has averted euro zone crisis.Things look good for now, but will it last?",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "Laughing_Gravy",
        "from_user_id": 16528102,
        "from_user_id_str": "16528102",
        "from_user_name": "Laughing Gravy\u2122",
        "geo": null,
        "id": 286437550313312256,
        "id_str": "286437550313312256",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/3027789460\/1bda0b2d9779e552e5d5f5dc7b46d7c1_normal.jpeg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/3027789460\/1bda0b2d9779e552e5d5f5dc7b46d7c1_normal.jpeg",
        "source": "<a href="http:\/\/blackberry.com\/twitter">Twitter for BlackBerry\u00ae<\/a>",
        "text": "@Lindow_Man  Happy New Year my friend :)",
        "to_user": "Lindow_Man",
        "to_user_id": 549313793,
        "to_user_id_str": "549313793",
        "to_user_name": "Mr PG",
        "in_reply_to_status_id": 286436483806670848,
        "in_reply_to_status_id_str": "286436483806670848"
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "ayinnisa",
        "from_user_id": 407600441,
        "from_user_id_str": "407600441",
        "from_user_name": "Arini Faranisa",
        "geo": null,
        "id": 286437550271381504,
        "id_str": "286437550271381504",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2527223229\/picmix-2172012-144555_normal.jpeg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2527223229\/picmix-2172012-144555_normal.jpeg",
        "source": "<a href="http:\/\/ubersocial.com">UberSocial for BlackBerry<\/a>",
        "text": "Happy nu year too my bicih :* RT @ThataMarsya: Happy new year couzy @tyaaaacw @ayinnisa @ayuayu2x @glori4valentine (\u02d8\u2323\u02d8)\u03b5\u02d8)",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "dougshepherd1",
        "from_user_id": 325897505,
        "from_user_id_str": "325897505",
        "from_user_name": "Doug Shepherd",
        "geo": null,
        "id": 286437550229434368,
        "id_str": "286437550229434368",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/1420764658\/dougandro_normal.jpg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/1420764658\/dougandro_normal.jpg",
        "source": "<a href="http:\/\/twitterfeed.com">twitterfeed<\/a>",
        "text": "Title Boxing Club 'Sweat Factory' Open For New Year's Resolution Help: When Ken Greer accidentally clicked a but... http:\/\/t.co\/RZ0Eim48",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "a1a2a2a3a4",
        "from_user_id": 935634619,
        "from_user_id_str": "935634619",
        "from_user_name": "\uc720\uc2b9\uc6b0\uc870\uc73c\ub2e4Vv",
        "geo": null,
        "id": 286437550145556480,
        "id_str": "286437550145556480",
        "iso_language_code": "ko",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2898657882\/6a71600348f1d4136866a86e267722d5_normal.png",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2898657882\/6a71600348f1d4136866a86e267722d5_normal.png",
        "source": "<a href="http:\/\/twitter.com\/download\/android">Twitter for Android<\/a>",
        "text": "RT @seungu97: @PutriNorizah \ubbf8\uc548\ud574\uc694\ud478\ud2b8\ub9aci was in a hurry to write you back happy new year \ud478\ud2b8\ub9ac\ub108\ubb34\ucc29\ud574\uc694\uce74\ud1a1\ud574\uc694",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "RaymondMarvinP",
        "from_user_id": 153311501,
        "from_user_id_str": "153311501",
        "from_user_name": "Raymond Pattipeilohy",
        "geo": null,
        "id": 286437550116184064,
        "id_str": "286437550116184064",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/3054427884\/86a9053b8bb8530cb31a730d6895abc3_normal.jpeg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/3054427884\/86a9053b8bb8530cb31a730d6895abc3_normal.jpeg",
        "source": "<a href="https:\/\/embr.in">embr<\/a>",
        "text": "Happy new year adk :D @khunkromo",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "Nellyestao",
        "from_user_id": 401253041,
        "from_user_id_str": "401253041",
        "from_user_name": "Nelly Estaorina",
        "geo": null,
        "id": 286437549428318209,
        "id_str": "286437549428318209",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/3012999534\/2e73bf044ef0282ab1829d9308609f8b_normal.jpeg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/3012999534\/2e73bf044ef0282ab1829d9308609f8b_normal.jpeg",
        "source": "<a href="http:\/\/blackberry.com\/twitter">Twitter for BlackBerry\u00ae<\/a>",
        "text": "RT @mutiaraulia: Happy New Year 2013 for UJP14\u263a\u2661 . Make it better! Better than 2012! Success for our training! And keep never remedial alone",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "katiee_douglas",
        "from_user_id": 231021947,
        "from_user_id_str": "231021947",
        "from_user_name": "Katie Douglas X",
        "geo": null,
        "id": 286437549407346688,
        "id_str": "286437549407346688",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/3049930519\/be6f7dd5d68c199df4c5befa121260d2_normal.jpeg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/3049930519\/be6f7dd5d68c199df4c5befa121260d2_normal.jpeg",
        "source": "<a href="http:\/\/twitter.com\/download\/iphone">Twitter for iPhone<\/a>",
        "text": "RT @CalvinTrever: Didn't sleep on new year, only slept last night so now I feel like f***ing s**t",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "hico2",
        "from_user_id": 205687874,
        "from_user_id_str": "205687874",
        "from_user_name": "hico\u00b2",
        "geo": null,
        "id": 286437549273124864,
        "id_str": "286437549273124864",
        "iso_language_code": "ja",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/1341047299\/DSC_4069_normal.JPG",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/1341047299\/DSC_4069_normal.JPG",
        "source": "<a href="http:\/\/twitter.com\/">web<\/a>",
        "text": "hico\u00b2\u30d6\u30ed\u30b0\u66f4\u65b0\u3057\u3083\u3063\u3059\uff01\u300eA HAPPY NEW YEAR\uff01\u301c\u7b11\u9580\u6765\u798f2013\u301c\uff01\uff01\u300f\nhttp:\/\/t.co\/Wk2lSVZJ",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "saionjikyua",
        "from_user_id": 154139162,
        "from_user_id_str": "154139162",
        "from_user_name": "\u304d\u3085\u3042K\u3000",
        "geo": null,
        "id": 286437549222817793,
        "id_str": "286437549222817793",
        "iso_language_code": "ja",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/3011151755\/914f0441f5cca86a822248ba84b125f0_normal.jpeg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/3011151755\/914f0441f5cca86a822248ba84b125f0_normal.jpeg",
        "source": "<a href="https:\/\/mobile.twitter.com">Mobile Web (M2)<\/a>",
        "text": "kyua HAPPY NEW YEAR!\"!!!!!\n\n\u897f\u5712\u5bfa\u308e\u305f\u3060\u3044\u307e\u65e5\u672c\u306b\u5e30\u5b85\u3057\u305f\u30ad\u30e5\u30a2\u2605",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:28 +0000",
        "from_user": "_GiuliaB_",
        "from_user_id": 161635884,
        "from_user_id_str": "161635884",
        "from_user_name": "Giulia Anna \u2665",
        "geo": null,
        "id": 286437548652376064,
        "id_str": "286437548652376064",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2396882564\/9ufvgyxf6hacuxl2znf7_normal.png",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2396882564\/9ufvgyxf6hacuxl2znf7_normal.png",
        "source": "<a href="http:\/\/www.hootsuite.com">HootSuite<\/a>",
        "text": "New Year's #resolutions http:\/\/t.co\/UGdMHPjf",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:27 +0000",
        "from_user": "WashingtonTroom",
        "from_user_id": 148635962,
        "from_user_id_str": "148635962",
        "from_user_name": "Washington Tea Room",
        "geo": null,
        "id": 286437548501377024,
        "id_str": "286437548501377024",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2211531785\/washington_tea_room_sign_1_normal.jpg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2211531785\/washington_tea_room_sign_1_normal.jpg",
        "source": "<a href="http:\/\/itunes.apple.com\/us\/app\/twitter\/id409789998?mt=12">Twitter for Mac<\/a>",
        "text": "Looking for a new year challenge? Why not volunteer to help for just a few hours each month for a local community project, very rewarding!",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:27 +0000",
        "from_user": "RosalynOlson11",
        "from_user_id": 1054839787,
        "from_user_id_str": "1054839787",
        "from_user_name": "Rosalyn Olson",
        "geo": null,
        "id": 286437548396527616,
        "id_str": "286437548396527616",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/3054422061\/cbb2cb2f2af48d452945aca59a66b073_normal.jpeg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/3054422061\/cbb2cb2f2af48d452945aca59a66b073_normal.jpeg",
        "source": "<a href="https:\/\/mobile.twitter.com">Mobile Web (M2)<\/a>",
        "text": "#job Anyone actually make this happen? http:\/\/t.co\/RkHSUrOk",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    },
    {
        "created_at": "Wed, 02 Jan 2013 11:43:27 +0000",
        "from_user": "BumperArmor",
        "from_user_id": 1021423430,
        "from_user_id_str": "1021423430",
        "from_user_name": "Bumper Armor",
        "geo": null,
        "id": 286437548350377984,
        "id_str": "286437548350377984",
        "iso_language_code": "en",
        "metadata": {
            "result_type": "recent"
        },
        "profile_image_url": "http:\/\/a0.twimg.com\/profile_images\/2993704676\/0647b39064b4ed7147be4a061e910bc3_normal.jpeg",
        "profile_image_url_https": "https:\/\/si0.twimg.com\/profile_images\/2993704676\/0647b39064b4ed7147be4a061e910bc3_normal.jpeg",
        "source": "<a href="http:\/\/twitter.com\/">web<\/a>",
        "text": "How was the New Year Celebration??",
        "to_user": null,
        "to_user_id": 0,
        "to_user_id_str": "0",
        "to_user_name": null
    }],
    "results_per_page": 15,
    "since_id": 0,
    "since_id_str": "0"
}

そして、次のJavaプログラムを使用してjsonファイルを解析しています

package jsonparser;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import java.util.Map.Entry;
public class BasicGenGson {

     final private String path = "C:/json/"; //set directory path
        private JsonParser parser = new JsonParser();
        private int count = -1;

        public static void main(String[] args) {
            BasicGenGson read = new BasicGenGson();
            read.sendJSONFile();
        }

        //send json files to read one by one
        private void sendJSONFile() {

            File[] jsonfiles = getJSONFiles();

            if (jsonfiles.length > 0) {
                for (int i = 0; i <= count; i++) {

                    try {
                        //prints json file names
                        System.out.println("File: \t" + jsonfiles[i]);
                        JsonElement jsonElement = parser.parse(new FileReader(jsonfiles[i]));
                        JsonObject jsonObject = jsonElement.getAsJsonObject();
                        readJSONFile(jsonObject);
                    }
                    catch (FileNotFoundException e) {
                        System.out.println(e.getMessage());
                        //e.getStackTrace();
                    }
                    catch (IOException e) {
                        System.out.println(e.getMessage());
                        //e.getStackTrace();
                    }
                    catch (Exception e) {
                        System.out.println(e.getMessage());
                        //e.getStackTrace();
                    }

                }
            }
        }

        //read a complete json file
        private void readJSONFile(JsonObject jsonObject) {

            for (Entry<String, JsonElement> entry : jsonObject.entrySet()) {

                String key = entry.getKey();
                JsonElement value = entry.getValue();

                if (value.isJsonObject()) {
                    readJSONFile(value.getAsJsonObject());
                }
                else if (value.isJsonArray()) {
                     JsonArray jsonArray = value.getAsJsonArray();

                     if (jsonArray.size() == 1) {
                        readJSONFile((JsonObject) jsonArray.get(0));
                    }
                     else {
                        //prints json array name
                        System.out.println(key);
                        Iterator<JsonElement> msg = jsonArray.iterator();
                        while (msg.hasNext()) {
                            ////prints json array values
                            //System.out.println(msg.next());
                            /*******************************************/
                            if (msg.next().isJsonObject()) {
                                readJSONFile(msg.next().getAsJsonObject());
                            }

                            /***********************************************/

                        }
                    }
                }
                else {
                    ////prints json object's keys and values
                    if (key.equals("from_user")|| key.equals("text") ) {
                        System.out.println(key + " - " + value);

                    }
//                  System.out.println(key + " - " + value);

                }
            }
        }

        //get only .json files from a directory
        private File[] getJSONFiles() {

            File folder = new File(path);
            File[] files = folder.listFiles();
            File[] jsonfiles = new File[files.length];

            for (int i = 0; i < files.length; i++) {

                if (files[i].isFile()) {
                    if (files[i].getName().endsWith(".json") || files[i].getName().endsWith(".JSON")) {
                        jsonfiles[++count] = files[i];
                    }
                }
            }

            return files;
        }

}

「from_user」と「text」を抽出する必要がありますが、すべてのフィールドを取得していません...いくつかの値の後、null が印刷されます。以下は出力です

File:   C:\json\NewYear.json
results
from_user - "ToddBuell"
text - "Fiscal cliff deal gives markets relief in New Year. \"Super Mario\" has averted euro zone crisis.Things look good for now, but will it last?"
from_user - "ayinnisa"
text - "Happy nu year too my bicih :* RT @ThataMarsya: Happy new year couzy @tyaaaacw @ayinnisa @ayuayu2x @glori4valentine (???)??)"
from_user - "a1a2a2a3a4"
text - "RT @seungu97: @PutriNorizah ???????i was in a hurry to write you back happy new year ????????????"
from_user - "Nellyestao"
text - "RT @mutiaraulia: Happy New Year 2013 for UJP14?? . Make it better! Better than 2012! Success for our training! And keep never remedial alone"
from_user - "hico2"
text - "hico²???????????A HAPPY NEW YEAR??????2013????\nhttp://t.co/Wk2lSVZJ"
from_user - "_GiuliaB_"
text - "New Year's #resolutions http://t.co/UGdMHPjf"
from_user - "RosalynOlson11"
text - "#job Anyone actually make this happen? http://t.co/RkHSUrOk"
null

助けてください...ありがとう

4

2 に答える 2

2

同じ理由で、JSON 配列内の他のすべてのエントリのみを出力しています。

if (msg.next().isJsonObject()) {
    readJSONFile(msg.next().getAsJsonObject());
}

Iteratornext()メソッドを 2 回呼び出しています。最初に呼び出すと、返されたオブジェクトが破棄されます。最後に、2 回目に呼び出すと、返さnullれるオブジェクトが配列に残っていないため、返されます。

于 2013-01-04T14:40:22.713 に答える