数据格式之解析Json

先看一篇文章:http://www.cnblogs.com/qianxudetianxia/archive/2011/07/22/2079979.html


这里就不转正文了,以后点击链接看看。。。

但有个问题,他介绍的几种格式并没有我想要的;从服务器上得到的json格式是这样的:

[{"description":"\u5e7f\u544a3","img":"http:\/\/clostyle2012.eicp.net\/images\/appimg\/ad\/3.jpg","link":"34"},{"description":"\u5e7f\u544a2","img":"http:\/\/clostyle2012.eicp.net\/images\/appimg\/ad\/2.jpg","link":"33"},{"description":"\u5e7f\u544a1","img":"http:\/\/clostyle2012.eicp.net\/images\/appimg\/ad\/1.jpg","link":"32"}]

一看就知道,外边就是一个JSONArray,元素是三个JSONObject对象。但是数组没有键,就不能按上面那篇文章讲的啦。我找了半天,实验了很多次,终于搞定了。但是不知道是不是最好的。

再放一篇文章:http://coolerbaosi.iteye.com/blog/1596261

他文章中的例子格式和我要的一样,我用了它代码中的一部分。。。


package com.Bill_Ming.csdn;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

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

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

//出现乱码是因为index3.jsp保存时不时utf-8格式的,以后用editplus编辑
public class JSONDemoActivity extends Activity {
    
	/**
	 * 访问的后台地址,这里访问本地的不能用127.0.0.1应该用10.0.2.2
	 */
	private static final String BASE_URL = "http://10.0.2.2:8080/index2.jsp";
	//private static final String BASE_URL ="http://clostyle2012.eicp.net/app/getAd";
	private TextView mStudentTextView;
	
	private TextView mClassTextView;
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try {
			setupViews();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
    
    /**
     * 初始化
     * @throws JSONException 
     * @throws IOException 
     */
    private void setupViews() throws JSONException, IOException{
    	mStudentTextView = (TextView)findViewById(R.id.student);
    	mClassTextView = (TextView)findViewById(R.id.classes);
    	
    byte []data = readParse(BASE_URL);
 
    JSONArray jarray = new JSONArray(new String(data));
     String studentInfo = "";
    for(int i = 0;i < jarray.length(); i++)
    {
    	JSONObject item = jarray.getJSONObject(i);
    	String name = item.getString("description");
    	studentInfo += " 描述 :";
    	studentInfo += name;
    	String address = item.getString("img");
    	studentInfo += " img :";
    	studentInfo += address;
    	int age = item.getInt("link");
    	studentInfo +=" link :";
    	studentInfo += age;
    	studentInfo += "\n";
    }
    	mStudentTextView.setText(studentInfo);
	
    }

	private byte[] readParse(String baseUrl) throws IOException {
		// TODO Auto-generated method stub
		ByteArrayOutputStream outStream = new ByteArrayOutputStream();
		byte[] data = new byte[1024];
		int len = 0;
		
		try {
			URL url = new URL(baseUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			InputStream inStream = conn.getInputStream();
			while( (len=inStream.read(data))  != -1){
				outStream.write(data, 0, len);
			}
			inStream.close();
			return outStream.toByteArray();
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return null;
	}
    
    
}



我用的是本地服务器,以上代码也是看别人的

参考:http://blog.csdn.net/android_tutor/article/details/7466620



还要加权限:<uses-permission android:name="android.permission.INTERNET"></uses-permission>

过程中遇到两次乱码情况,

第一次:是浏览器中输入url,出现乱码,解决方法:在tomcat 安装文件找到conf文件夹的server.xml中 改成如下这样,加个 URlEncoding = "GBK",还要保证你的浏览器编码也是GBK的

<Connector port="8080" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" URIEncoding="GBK" />

第二次是写了那个json数据的jsp文件,在模拟器中运行时乱码,解决方法:你的jsp文件保存时要保存成utf-8格式就好了,推荐用editplus



这三篇文章都是经典啊



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值