0

我只是从服务器解析 Json 格式的数据。我被卡住了。我不知道如何进一步进行。

JSONObject object1 = new JSONObject(str);
String s1 = object1.getString("results");
JSONArray array1 = object1.getJSONArray("constituencies");

上面的代码对吗?

4

2 回答 2

0

看看下面的 JSON Parsing 代码,

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);
}

原始来源

于 2013-03-29T08:19:10.567 回答
0

Above codes right or wrong??

So look at source:

{
   "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"
         },
      ]
   }
}

Here you have two JSONObject:

  • First(whole source)
  • results

results contains(in this snippet of code) two JSONArrays:

  • constituencies(that is empty array)
  • members(it has two childs)

So you need to first fetch whole JSON then results and then you have access to arrays.

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");
   ...

}
于 2013-03-29T08:19:22.443 回答