2

私はJSONに非常に慣れていません。Java を使用して、JSON ファイル内の 1 つの要素を出力することができました。ただし、ファイルが複数の要素で構成されている場合、それを取得する方法がわかりません。JSONArray を使用する必要がありますか? JSONArray を検索して適用しようとしましたが、方法がわかりませんでした。それともGson、Jacksonのことと関係がありますか? 私は彼らが何であるか分かりません..

これは私のexample.jsonファイルです:

{"rel": "/r/AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "/e/ec1914bfb0606c36376fbbcd316e5666022e2469", "features": ["/c/en/apple_inc /r/AtLocation -", "/c/en/apple_inc - /c/en/unite_state", "- /r/AtLocation /c/en/unite_state"], "end": "/c/en/unite_state", "license": "/l/CC/By-SA", "uri": "/a/[/r/AtLocation/,/c/en/apple_inc/,/c/en/unite_state/]", "start": "/c/en/apple_inc", "context": "/ctx/all", "surfaceText": null}
{"rel": "/r/IsA", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "/e/914e6775fd79d660bacf22ec699568e6694da3e8", "features": ["/c/en/america_beautiful /r/IsA -", "/c/en/america_beautiful - /c/en/national_anthem", "- /r/IsA /c/en/national_anthem"], "end": "/c/en/national_anthem", "license": "/l/CC/By-SA", "uri": "/a/[/r/IsA/,/c/en/america_beautiful/,/c/en/national_anthem/]", "start": "/c/en/america_beautiful", "context": "/ctx/all", "surfaceText": null}

これは私のJavaファイルです:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class test
{
    public static void main(String[] args)
    {

        JSONParser parser = new JSONParser();

        try
        {

            Object obj = parser.parse(
                new FileReader("C:/Users/LijingYeo/Desktop/example.json"));

            JSONObject jsonObject = (JSONObject) obj;

            String rel = (String) jsonObject.get("rel");
            System.out.println(rel);

            String start = (String) jsonObject.get("start");
            System.out.println(start);

            String end = (String) jsonObject.get("end");
            System.out.println(end);

        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        catch (ParseException e)
        {
            e.printStackTrace();
        }

    }

}

あなたの時間と努力に感謝します!

4

3 に答える 3

4

jsonファイルを1行ずつ読み取ってから、jsonオブジェクトに解析する必要があると思います。このサンプル コードは、json ファイルに対して機能します。

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class ReadJsonFile
{
    public static void main(String[] args)
    {        
        readJsonFile();
    }

    public static void readJsonFile() {

        BufferedReader br = null;
        JSONParser parser = new JSONParser();

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("D:\\example.json"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println("Record:\t" + sCurrentLine);

                Object obj;
                try {
                    obj = parser.parse(sCurrentLine);
                    JSONObject jsonObject = (JSONObject) obj;

                    String rel = (String) jsonObject.get("rel");
                    System.out.println(rel);

                    String start = (String) jsonObject.get("start");
                    System.out.println(start);

                    String end = (String) jsonObject.get("end");
                    System.out.println(end);

                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}

Json ファイルは以下のとおりです。これが役立つことを願っています。

于 2012-09-18T03:48:51.707 に答える
0

ObjectMapperを使用できます。

String jsonString = null;
if (prettyPrint == true) {
    // this is time consuming
    jsonString = myObjectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(myObject);
} else {
    // faster option
    jsonString = myObjectMapper.writeValueAsString(myObject);
}
于 2012-09-18T04:07:27.567 に答える
0

あなたがしていることは何でも正しいです。

JSON 構造は type JSONObject、つまり (Key, Value) ペアです。これらの要素のうち、features要素の値の型はJSONArrayです。そのため、要素をブラウズしているときに、要素の値をキャストしてfeaturesからJSONArray、その配列内の要素のリストを反復処理する必要があります。

JSONArray features = (JSONArray) jsonObject.get("features");
int size = features.size();
for (int i = 0; i < size; i++)
{
    System.out.println(features.get(i));
}

HTH。

于 2012-09-18T02:25:25.273 に答える