{
"blogURL": "http://crunchify.com",
"twitter": "http://twitter.com/Crunchify",
"social": [{
"id": "1",
"message": "hi"
},
{
"id": "2",
"message": "hello"
}]
}
我正在尝试使用下面给出的代码阅读上述.txt文件。
package com.srishti.space_om;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import org.json.JSONException;
import org.json.JSONObject;
public class SPACE_Parse {
public static void main(String[] args) throws FileNotFoundException, JSONException {
String jsonData = "";
BufferedReader br = null;
try {
String line;
br = new BufferedReader(new FileReader("C:/Users/Rachana/workspace/SPACEOM/WebContent/Data/Parse_JSON.txt"));
while ((line = br.readLine()) != null) {
jsonData += line + "\n";
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
// System.out.println("File Content: \n" + jsonData);
JSONObject obj = new JSONObject(jsonData);
System.out.println("blogURL: " + obj.getString("blogURL"));
System.out.println("twitter: " + obj.getString("twitter"));
}
}
我能够阅读blogURL和twitter但我怎样才能阅读第一个社交数组,即我必须能够阅读社交[0] [id]并获得1和社交[1] [id] = 2。 / p>
答案 0 :(得分:0)
JSONArray array = obj.getJSONArray("social");
for (int i = 0; i < array.length(); i++) {
JSONObject row = array.getJSONObject(i);
id = row.getInt("id");
message= row.getString("message");
}