zoukankan      html  css  js  c++  java
  • Android中解析JSON数据流

            在上一篇文章中,我用到的是第三方的类库Gson来解析Json数据流,经过仔细研究,发现Android平台自己的类里也有比较好使用的解析方法。虽然JsonReader这个类是SDK3.0以后才能用的,但这里可以用JSONArray类获取一个JSON对象数组,然后逐个获得JSONObject,进而获得自己需要的信息。

    相关代码如下:

    public class MainActivity extends Activity {
        private TextView textView;
     
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            textView = (TextView) findViewById(R.id.textView);
            String data = null;
            String result = "";
            try {
                data = HttpUtils
                        .getData("http://10.16.12.165:8080/JsonTest/JsonTestServlet");
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                JSONArray array = new JSONArray(data);
                for (int i = 0; i < array.length(); i++) {
                    JSONObject object = array.getJSONObject(i);
                    result += "name: " + object.getString("name") + "  age: "
                            + object.getInt("age") + "   id: "
                            + object.getString("id") + "\n";
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            textView.setText(result);
     
        }
  • 相关阅读:
    数据库ALL和ANY的区别
    数据库-关系代数-投影
    数据库关系代数表达式学习
    数据模型的三要素
    题解 P2812 【校园网络【[USACO]Network of Schools加强版】】
    题解 P2746 【[USACO5.3]校园网Network of Schools】
    题解 P2257 【YY的GCD】
    题解 P6476 【[NOI Online #2 提高组]涂色游戏】
    题解 P2522 【[HAOI2011]Problem b】
    题解 P4782 【【模板】2-SAT 问题】
  • 原文地址:https://www.cnblogs.com/yangzhenyu/p/2229496.html
Copyright © 2011-2022 走看看