我只是从服务器解析数据,这是Json格式的。我被困了。我不知道如何继续前进。
JSONObject object1 = new JSONObject(str);
String s1 = object1.getString("results");
JSONArray array1 = object1.getJSONArray("constituencies");
上面的代码是对的吗?
答案 0 :(得分:0)
查看以下JSON解析代码,
public class ParseJSON extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String readTwitterFeed = readTwitterFeed();
try {
JSONArray jsonArray = new JSONArray(readTwitterFeed);
Log.i(ParseJSON.class.getName(),
"Number of entries " + jsonArray.length());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
Log.i(ParseJSON.class.getName(), jsonObject.getString("text"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String readTwitterFeed() {
StringBuilder builder = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://twitter.com/statuses/user_timeline/vogella.json");
try {
HttpResponse response = client.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if (statusCode == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
}
} else {
Log.e(ParseJSON.class.toString(), "Failed to download file");
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return builder.toString();
}
}
编写JSON非常简单。只需创建JSONObject或JSONArray并使用toString()方法。
public void writeJSON() {
JSONObject object = new JSONObject();
try {
object.put("name", "Jack Hack");
object.put("score", new Integer(200));
object.put("current", new Double(152.32));
object.put("nickname", "Hacker");
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(object);
}
答案 1 :(得分:0)
以上代码是对还是错?
所以看看来源:
{
"results":{
"constituencies":[
],
"members":[
{
"constituency_name":"Beckenham",
"member_name":"Bob Stewart",
"member_party":"Conservative",
"member_biography_url":"http://www.parliament.uk/biographies/commons/Bob-Stewart/3919",
"member_website":"http://www.bobstewartmp.com/",
"uri":"http://findyourmp.parliament.uk/constituencies/beckenham.json"
},
{
"constituency_name":"Colchester",
"member_name":"Sir Bob Russell",
"member_party":"Liberal Democrat",
"member_biography_url":"http://www.parliament.uk/biographies/commons/Sir-Bob-Russell/35",
"member_website":"http://www.bobrussell.org.uk",
"uri":"http://findyourmp.parliament.uk/constituencies/colchester.json"
},
]
}
}
这里有两个JSONObject
:
结果包含(在此代码段中)两个JSONArrays
:
因此,您需要先获取整个JSON
然后获取结果,然后才能访问数组。
JSONObject root = new JSONObject(str); // whole source
JSONObject results = root.getJSONObject("results");
JSONArray constituenciesArr = results.getJSONArray("constituencies");
JSONArray membersArr = results.getJSONArray("members");
JSONObject child = null;
for (int i = 0; i < membersArr.length(); i++) {
child = membersArr.getJSONObject(i);
String mName = child.getString("member_name");
...
}