当前位置:编程学习 > JS >>

数据格式之解析Json

这里就不转正文了,以后点击链接看看。。。
但有个问题,他介绍的几种格式并没有我想要的;从服务器上得到的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对象。但是数组没有键,就不能按上面那篇文章讲的啦。我找了半天,实验了很多次,终于搞定了。但是不知道是不是最好的。
 他文章中的例子格式和我要的一样,我用了它代码中的一部分。。。

[java]
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; 
    } 
     
     

补充:web前端 , JavaScript ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,