使用gson-1.6.jar解析json

package com.example.mars_2900_json01;

import java.io.StringReader;
import java.lang.reflect.Type;
import java.util.Iterator;
import java.util.LinkedList;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;

public class JsonUtils {
    public static void parseJson(String jsonData) {
        try {
            // 如果需要解析JSON数据,首要要生成一个JsonReader对象
            //"[{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}]"
            JsonReader reader = new JsonReader(new StringReader(jsonData));
            reader.beginArray();//开始解析数组
            while (reader.hasNext()) {
                reader.beginObject();//开始解析对象
                while (reader.hasNext()) {
                    String tagName = reader.nextName();
                    if (tagName.equals("name")) {
                        System.out.println("name--->" + reader.nextString());
                    } else if (tagName.equals("age")) {
                        System.out.println("age--->" + reader.nextInt());
                    }
                }
                reader.endObject();
            }
            reader.endArray();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void parseUserFromJson(String jsonData){
        Gson gson = new Gson();
        User user = gson.fromJson(jsonData, User.class);
        System.out.println("name--->" + user.getName());
        System.out.println("age---->" + user.getAge());
    }
    
    public static void parseUserListFromJson(String jsonData){
        Type listType = new TypeToken<LinkedList<User>>(){}.getType();
        Gson gson = new Gson();
        LinkedList<User> users = gson.fromJson(jsonData, listType);
        for (Iterator iterator = users.iterator(); iterator.hasNext();) {
            User user = (User) iterator.next();
            System.out.println("name--->" + user.getName());
            System.out.println("age---->" + user.getAge());
        }
    }
}
复制代码

 

复制代码
package com.example.mars_2900_json01;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
    private String jsonData = "[{\"name\":\"Michael\",\"age\":20},{\"name\":\"Mike\",\"age\":21}]";
    private String jsonData2 = "{\"name\":\"json\",\"age\":21}";
    private Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.buttonId);
        button.setOnClickListener(new ButtonListener());
    }

    private class ButtonListener implements OnClickListener {

        @Override
        public void onClick(View v) {
            JsonUtils.parseJson(jsonData);
            JsonUtils.parseUserFromJson(jsonData2);
            JsonUtils.parseUserListFromJson(jsonData);
            
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}
posted @ 2016-04-06 09:19  疯子110  阅读(354)  评论(0编辑  收藏  举报