2014年1月9日 星期四

JAVA的JSON相關操作

JAVA的JSON相關操作


 


由於工作需要,所以找到相關的程式碼,趕緊備份下來,歡迎有興趣的同好,一起來C/P一下。


 









package com.example.framelayout_parsejson;

import android.os.Bundle;
import android.app.Activity;
import android.text.TextUtils.TruncateAt;
import android.util.Log;
import android.view.Menu;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

///////////////////////////////////////
import android.view.View;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.PopupWindow;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.VideoView;
import android.view.*;
import android.view.View.*;
import android.widget.*;
import android.view.ViewGroup.MarginLayoutParams;
import android.app.ActionBar.LayoutParams;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
public class MainActivity extends Activity {
FrameLayout FrameLayout1;
DisplayMetrics metrics;
TextView TextView1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////////////////////////////////
requestWindowFeature(Window.FEATURE_NO_TITLE); //全螢幕設定
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
metrics = new DisplayMetrics();//抓取螢幕大小
getWindowManager().getDefaultDisplay().getMetrics(metrics);

/////////////////////////////////////////
FrameLayout1 = new FrameLayout(this);
FrameLayout.LayoutParams lparam1 = new FrameLayout.LayoutParams(metrics.widthPixels,metrics.heightPixels);
FrameLayout1.setLayoutParams(lparam1);
this.setContentView(FrameLayout1);

////////////////////////////////////////
TextView1=new TextView(this);
FrameLayout.LayoutParams layoutParams_TextView = new FrameLayout.LayoutParams(metrics.widthPixels, metrics.heightPixels);
layoutParams_TextView.gravity = Gravity.LEFT|Gravity.TOP;
layoutParams_TextView.leftMargin = 0;//X
layoutParams_TextView.topMargin = 0;//Y

TextView1.setLayoutParams(layoutParams_TextView);
TextView1.setText("");

FrameLayout1.addView(TextView1);

try {
JSONEncode();
JSONDecode();
} catch (JSONException e) {
e.printStackTrace();
}
}
private void JSONEncode() throws JSONException {
JSONArray jsonArray = new JSONArray();

for (int i = 0; i < name.length; i++) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", name[i]);
jsonObject.put("id", id[i]);
jsonObject.put("score", score[i]);
jsonArray.put(jsonObject);
}

TextView1.append(jsonArray.toString());
TextView1.append("\n");
TextView1.append("////////////////////////\n");
JSONString = jsonArray.toString();
}

private void JSONDecode() throws JSONException {
JSONArray jsonArray = new JSONArray(JSONString);
Log.i("Number of Entries", Integer.toString(jsonArray.length()));

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
String name = jsonObject.getString("name");
int id = jsonObject.getInt("id");
double score = jsonObject.getDouble("score");
TextView1.append("name: " + name + ", id: " + id + ", score: " + score);
TextView1.append("\n");
}
}
private String JSONString;
private static final String[] name = { "Android", "Ben", "Chris", "David", "Eric", "Frankie",
"Gary", "Henry", "Ivan", "Joe" };
private static final int[] id = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
private static final double[] score = { 4.8, 3.2, 2.1, 4.3, 2.0, 0.9, 1.4, 2.8, 3.4, 0.1 };
@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;
}

}


 


沒有留言:

張貼留言