Ich versuche, den „Definition“ String aus dem folgenden JSON-Schnipsel zu erhalten:Wie mache ich einen String vom JSON-Objekt?
{
"headword": "Google",
"senses": [
{
"definition": [
"a very popular search enginecomputer program that allows you to search for information on the Internet. Google also provides other Internet services including e-mail, maps, social networking, and video sharing."
]
}
],
"datasets": [
"ldoce5",
"dictionary"
],
"id": "cqAFJKTgNJ",
"url": "/v2/dictionaries/entries/cqAFJKTgNJ",
"pronunciations": [
{
"audio": [
{
"lang": "British English",
"type": "pronunciation",
"url": "/v2/dictionaries/assets/ldoce/gb_pron/google1004.mp3"
},
{
"lang": "American English",
"type": "pronunciation",
"url": "/v2/dictionaries/assets/ldoce/us_pron/google1004a.mp3"
}
],
"ipa": "ˈɡuːɡəl"
}
]
}
Das Problem ist im aktuellen Snippet „Definition“ JSON, dass ein JSONObject und zu sein scheint, wenn ich versuche, mach es String, ich bekomme einen Fehler, der besagt, dass es kein String ist. Wenn ich nur verlassen, ein Objekt wie die Ausgabe enthält Klammern und andere Symbole:
{"definition":["a very popular search enginecomputer program that allows you to search for information on the Internet. Google also provides other Internet services including e-mail, maps, social networking, and video sharing."]}
Gibt es eine Möglichkeit, einen schönen und sauberen String zu machen, die genauere Definition und nichts enthalten würde. BTW, mit org.json Bibliothek.
Code:
public class MainActivity {
private static String word;
public static void wordInitializer() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
word = reader.readLine();
}
public static void processing() throws Exception {
String path = "http://api.pearson.com/v2/dictionaries/ldoce5/entries?headword=" + word;
URL url = new URL(path);
Scanner scanner = new Scanner(url.openStream());
String line = "";
while (scanner.hasNext())
line += scanner.nextLine();
scanner.close();
JSONObject obj = new JSONObject(line);
JSONObject results = obj.getJSONArray("results").getJSONObject(0);
JSONArray senses = results.getJSONArray("senses");
String definition = senses.getJSONObject(0).toString();
System.out.println(definition);
}
public static void main(String[] args) throws Exception {
wordInitializer();
processing();
}
wo Ihr Code – redFIVE
Es ist ein JSON-Array mit einem einzelnen Element, ein JSON-String. Bitte beachten Sie das JSON-Format [hier] (http://json.org/). –
Definition ist ein Element in der JSON mit einem Array .... –