android平台上的json解析
来源:互联网 发布:农村淘宝考试题和答案 编辑:程序博客网 时间:2024/06/07 18:02
json数据格式解析分为两种:一种是普通的,一种是带有数组形式的。
普通形式的:服务器端返回的json数据格式如下:
{"userbean":{"Uid":"100196","Showname":"\u75af\u72c2\u7684\u7334\u5b50","Avtar":null,"State":1}}
带数组形式的:服务器端返回的数据格式为:
{"calendar":
{"calendarlist":
[
{"calendar_id":"1705","title":"(\u4eb2\u5b50)ddssd","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288927800","endshowtime":"1288931400","allDay":false},
{"calendar_id":"1706","title":"(\u65c5\u884c)","category_name":"\u9ed8\u8ba4\u5206\u7c7b","showtime":"1288933200","endshowtime":"1288936800","allDay":false}
]
}
}
所以解析上面两种格式的实例如下:
package pan.gch.demo;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private String simpleJSON = "{'userbean':{'Uid':'100196','Showname':'\u75af\u72c2\u7684\u7334\u5b50','Avtar':null,'State':1}}";
private String complexJSON = "{'calendar':{'calendarlist':[{'calendar_id':'1705','title':'(\u4eb2\u5b50)ddssd','category_name':'\u9ed8\u8ba4\u5206\u7c7b','showtime':'1288927800','endshowtime':'1288931400','allDay':false},{'calendar_id':'1706','title':'(\u65c5\u884c)','category_name':'\u9ed8\u8ba4\u5206\u7c7b','showtime':'1288933200','endshowtime':'1288936800','allDay':false}]}}";
private TextView mSimpleJSONTextView;
private TextView mComplexJSONTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSimpleJSONTextView = (TextView) findViewById(R.id.textView1);
mComplexJSONTextView = (TextView) findViewById(R.id.textView2);
try {
JSONObject simpleJsonObject = new JSONObject(simpleJSON)
.getJSONObject("userbean");
String uidString = simpleJsonObject.getString("Uid");
String shownameString = simpleJsonObject.getString("Showname");
String avtarString = simpleJsonObject.getString("Avtar");
int state = simpleJsonObject.getInt("State");
mSimpleJSONTextView.setText(uidString + "\t" + shownameString
+ "\t" + avtarString + "\t" + state);
JSONObject complexObject = new JSONObject(complexJSON)
.getJSONObject("calendar");
String complexString = "";
for (int i = 0; i < complexObject.getJSONArray("calendarlist")
.length(); i++) {
JSONObject complexJsonObject = (JSONObject) (complexObject
.getJSONArray("calendarlist").opt(i));
String complexItemString = null;
String calendar_id = complexJsonObject.getString("calendar_id");
String titleString = complexJsonObject.getString("title");
String categoryString = complexJsonObject
.getString("category_name");
String showString = complexJsonObject.getString("showtime");
String endShowString = complexJsonObject
.getString("endshowtime");
Boolean allDay = complexJsonObject.getBoolean("allDay");
complexItemString = calendar_id + "\t" + titleString + "\t"
+ categoryString + "\t" + showString + "\t" + "\t"
+ endShowString + "\t";
if (allDay == true) {
complexItemString += "true";
} else {
complexItemString += "false";
}
complexString += (complexItemString + "\n");
}
mComplexJSONTextView.setText(complexString);
} catch (JSONException e) {
// TODO Auto-generated catch block
System.out.println("-------------error" + e);
e.printStackTrace();
}
}
}
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private String simpleJSON = "{'userbean':{'Uid':'100196','Showname':'\u75af\u72c2\u7684\u7334\u5b50','Avtar':null,'State':1}}";
private String complexJSON = "{'calendar':{'calendarlist':[{'calendar_id':'1705','title':'(\u4eb2\u5b50)ddssd','category_name':'\u9ed8\u8ba4\u5206\u7c7b','showtime':'1288927800','endshowtime':'1288931400','allDay':false},{'calendar_id':'1706','title':'(\u65c5\u884c)','category_name':'\u9ed8\u8ba4\u5206\u7c7b','showtime':'1288933200','endshowtime':'1288936800','allDay':false}]}}";
private TextView mSimpleJSONTextView;
private TextView mComplexJSONTextView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSimpleJSONTextView = (TextView) findViewById(R.id.textView1);
mComplexJSONTextView = (TextView) findViewById(R.id.textView2);
try {
JSONObject simpleJsonObject = new JSONObject(simpleJSON)
.getJSONObject("userbean");
String uidString = simpleJsonObject.getString("Uid");
String shownameString = simpleJsonObject.getString("Showname");
String avtarString = simpleJsonObject.getString("Avtar");
int state = simpleJsonObject.getInt("State");
mSimpleJSONTextView.setText(uidString + "\t" + shownameString
+ "\t" + avtarString + "\t" + state);
JSONObject complexObject = new JSONObject(complexJSON)
.getJSONObject("calendar");
String complexString = "";
for (int i = 0; i < complexObject.getJSONArray("calendarlist")
.length(); i++) {
JSONObject complexJsonObject = (JSONObject) (complexObject
.getJSONArray("calendarlist").opt(i));
String complexItemString = null;
String calendar_id = complexJsonObject.getString("calendar_id");
String titleString = complexJsonObject.getString("title");
String categoryString = complexJsonObject
.getString("category_name");
String showString = complexJsonObject.getString("showtime");
String endShowString = complexJsonObject
.getString("endshowtime");
Boolean allDay = complexJsonObject.getBoolean("allDay");
complexItemString = calendar_id + "\t" + titleString + "\t"
+ categoryString + "\t" + showString + "\t" + "\t"
+ endShowString + "\t";
if (allDay == true) {
complexItemString += "true";
} else {
complexItemString += "false";
}
complexString += (complexItemString + "\n");
}
mComplexJSONTextView.setText(complexString);
} catch (JSONException e) {
// TODO Auto-generated catch block
System.out.println("-------------error" + e);
e.printStackTrace();
}
}
}
执行结果如下图:
0 0
- android平台上的json解析
- QT平台上的Json解析
- QT平台上的Json解析
- QT平台上的Json解析
- android平台上的json解析 && 基站定位原理和GPS定位实例 && android RIL解析
- JSON解析-android平台
- JSON解析-android平台2
- JSON解析-android平台3
- JSON解析-android平台4
- Android上用Jackson Json解析json
- android如何解析网络上的json数据。
- Android平台上的XML解析技术分析
- Android的JSON解析
- Android的Json解析
- Android的JSON解析
- Android的JSON解析
- android json的解析
- android json的解析
- python操作XML文件------elementtree
- 《数字图像处理》——图像的一阶微分之图像梯度
- Linux多线程中使用信号-1
- Linux2.6用户态API HooK手动操作
- C++ 三目运算符
- android平台上的json解析
- 《疯狂的号码》全网上线 揭“号码大战”内幕
- SIGPIPE信号处理
- hibernate4 spring3 整合
- 黑马程序员-Foundation的使用
- 随笔(2014.12)
- C++读取excel表格
- DDoS攻击防御方案
- 去掉UINavigationBar下面黑线方法