JSON02

package mars.json02;

import android.R;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends ActionBarActivity {

	private String jsonData = "[{\"name\":\"Micheal\"},{\"name\":\"Mike\":21}]";
	private Button button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_item);
		button = (Button)findViewById(R.id.button1);
		button.setOnClickListener(new ButtonListener());
	}
	
	private class ButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			JsonUtils jsonUtils = new JsonUtils();
			jsonUtils.parseUserFromJson(jsonData);
		}
		
	}

}


package mars.json02;

import com.google.gson.Gson;

public class JsonUtils {

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


package mars.json02;

public class User {
	private String name;
	private int age;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}

}

你可能感兴趣的:(java,json)