Androidはjsonデータの解析とjsonフォーマットに変換する文字列を実現する

5349 ワード

Android sdkの中の
JSONObjectとJSOnArrayは集合または通常データをjson形式の文字列に変換する
JSOnObjectとJSOnArray解析json形式の文字列が集合または一般データ
import java.util.ArrayList;
import java.util.List;
 
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
 
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends Activity implements OnClickListener {
private Button button1, button2, button3, button4; //4   
private TextView textView; //       textview
private List userBeans; //       
private JSONObject object; //JSONObject  ,         
private JSONObject object2;
private JSONArray jsonArray;//JSONObject  ,            
private String jsonString; //      json   
private String jsonString2;//     json   
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initView(); //     
initDate(); //     
setListener(); //     
 
}
 
private void initView() {
button1 = (Button) findViewById(R.id.bt1);
button2 = (Button) findViewById(R.id.bt2);
button3 = (Button) findViewById(R.id.bt3);
button4 = (Button) findViewById(R.id.bt4);
textView = (TextView) findViewById(R.id.text);
}
/**
*    2     
*/
private void initDate() {
userBeans = new ArrayList();
UserBean userBean = new UserBean();
userBean.setUserId(1);
userBean.setUserName("hck");
userBeans.add(userBean);
 
UserBean userBean2 = new UserBean();
userBean2.setUserId(2);
userBean2.setUserName("  ");
userBeans.add(userBean2);
 
}
 
private void setListener() {
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
button4.setOnClickListener(this);
}
 
private void changeArrayDateToJson() { //        json      
jsonArray=null;
object=null;
jsonArray = new JSONArray();
object=new JSONObject();
for (int i = 0; i < userBeans.size(); i++) { //            ,     JSONObject  
object2 = new JSONObject();//  user  ,    JSONObject    
try {
object2.put("userId", userBeans.get(i).getUserId()); //       ,  JSONObject   JSONObject   map     ,          
object2.put("userName", userBeans.get(i).getUserName());
jsonArray.put(object2); // JSONObject    jsonArray    
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
object.put("userDate", jsonArray); //  JSONArray    JSONObject    (      )
//object.put("time", "2013-11-14"); //         ,  json    ,     ,      
} catch (JSONException e) {
e.printStackTrace();
}
jsonString=null;
jsonString = object.toString(); // JSONObject   json      
textView.setText(jsonString);
Log.i("hck", "   json   : " + jsonString);
 
}
private void changeNotArrayDateToJson() {
object=null;
object=new JSONObject();
try {
object.put("userId", "1"); //     JSONObject    ,"userid"   map   key,1  value  。
object.put("userName", "hck");
} catch (JSONException e) {
e.printStackTrace();
}
jsonString2=null;
jsonString2 = object.toString();// JSONObject   json      
Log.i("hck", "   json   : " + jsonString2);
textView.setText(jsonString2);
}
 
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
changeNotArrayDateToJson(); //       ,      json     string
break;
 
case R.id.bt2:
changeArrayDateToJson(); //   2   ,       json     string
break;
case R.id.bt3: //       json   
if (jsonString2==null || "".equals(jsonString2)) {
Toast.makeText(MainActivity.this, "       1         json   ", Toast.LENGTH_LONG).show();
return;
}
changeJsonToData2();
break;
case R.id.bt4://      json   
if (jsonString==null || "".equals(jsonString)) {
Toast.makeText(MainActivity.this, "     2       json   ", Toast.LENGTH_LONG).show();
return;
}
changeJsonToData1();
break;
default:
break;
}
 
}
private void changeJsonToData1()
{
StringBuilder stringBuilder=new StringBuilder(); //            ,   textview
UserBean userBean;
List bList=new ArrayList();
try {
object=new JSONObject(jsonString); // json          JSONObject  
jsonArray=object.getJSONArray("userDate"); //  key,  JSONObject     JSONArray  
for (int i = 0; i < jsonArray.length(); i++) { //    
object=jsonArray.getJSONObject(i); // JSONArray      JSONObject  
userBean=new UserBean();
userBean.setUserId(object.getInt("userId")); //  key,       
userBean.setUserName(object.getString("userName"));
bList.add(userBean);
}
} catch (JSONException e) {
e.printStackTrace();
}
for (int i = 0; i < bList.size(); i++) {
stringBuilder.append("  id:"+bList.get(i).getUserId()).append(" ").append("    :"+bList.get(i).getUserName());
}
textView.setText(stringBuilder.toString().replace("null", ""));
}
private void changeJsonToData2()
{
try {
object=new JSONObject(jsonString2);
String userName=object.getString("userName");
String userIdString=object.getString("userId");
textView.setText("  id"+userIdString+"    :"+userName);
} catch (JSONException e) {
e.printStackTrace();
}
}
}