android项目文件简介 及JSON文件解析

这篇博客介绍了Android项目的文件结构,重点讲解了如何解析JSON文件并将其数据展示在ListView中。通过`AssetManager`读取JSON文件,使用`JSONObject`和`JSONArray`进行解析,然后将解析结果用ArrayAdapter填充到ListView。此外,还展示了如何在不同Activity之间传递数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
1  src
存放源文件的地方
2.  gen
自动生成的文件
3  android 2.1 的库文件
4assets 存放外部文件的地方如json文件
4   res
里面有layout ,布局,控件可以画上去的那种
values定义标题名字的地方可以
AndroidManifest.xml 这个文件也很重要,在里面设置新的active的地方。
 
package com.android.ling;



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

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



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;



public class json extends Activity 
{
	private JSONObject dataJson=null;
	private JSONObject nbaJson=null;
	private String TAG="com.android.ling"; 
	private ListView lv=null;
	private String str=new String();
	private String temp;
	private String [] result=null;
	private InputStream fil = null;
	private BufferedReader br=null;
	@Override
public void onCreate(Bundle savedInstanceState) 
{
   /* try {
		Thread.sleep(15000,0);
	} catch (InterruptedException e1) {
		// TODO Auto-generated catch block
		e1.printStackTrace();
	}*/
    super.onCreate(savedInstanceState);
    setContentView(R.layout.index);
  
      	
    	try{
    		fil = getAssets().open("list.json");
    	}catch(IOException e){
    		e.printStackTrace();
    		Log.d(TAG,"=========="+fil);  
    	}
    	
    	
    	
    	try{
    		 br=new BufferedReader(new InputStreamReader(fil,"UTF-8"));
    	}catch (UnsupportedEncodingException e)  {

    		 e.printStackTrace();
    		 Log.d(TAG,"=========="+br);  
    	}
    	
    	
    	try{
    		while(br!=null&&null!=(temp=br.readLine()))
    			{
    			str+=temp; 
    			if(null!=str)
    			Log.d(TAG,"str is =========="+str); 
    			}
    	
    	}catch(IOException e){
    		e.printStackTrace();
    		Log.d(TAG,"=========="+str);  
    	}
    	
    	
    	try{
    		
    		 dataJson = new JSONObject(str);	
    			
    		 if(null!=dataJson) 
    			
   				 Log.d(TAG,"datajson is=========="+dataJson);    		
    		 
    	    		 
        }catch (JSONException e){
	  
    		e.printStackTrace();
    		 Log.d(TAG," datajson is =========="+dataJson); 
    		}			
	  
    	
    	  JSONArray js= dataJson.names();
    	  result=new String[js.length()];
          for(int i =0;i<js.length();i++)
          {
        	
        	    result[i]=js.optString(i);//解析JSON文件,
          }
        
        
    	  lv=(ListView)findViewById(R.id.listView);
    	    ListAdapter adapter=new ArrayAdapter<String>(
    	    		this,android.R.layout.simple_list_item_1 ,
    	    		result);
    	    lv.setAdapter(adapter);
    	    
    	    lv.setOnItemClickListener(new OnItemClickListener()
    	    {    
    	    
    	    	@Override
    			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    					long arg3) {
    				    	    		
    	    		  String map = arg0.getItemAtPosition(arg2).toString();
    	             
    	    		 Intent i = new Intent();
    	        	   
    	             i.setClass(json.this, json1.class);
    	             
    	             Bundle bundle=new Bundle();
    	             bundle.putString("KEY_TEXT", map);
    	             bundle.putString("KEY_Object", str); //向另一个active传值 
    	             
    	             i.putExtras(bundle);
    	             
    	             startActivity(i);
    			}
    			
    	    }
    	      );
    	   
    	
   }
}

 
package com.android.ling;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.LinkedList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;


public class json1 extends Activity 
{
	private ListView lv=null;
	private String TAG="com.android.ling"; 
	private JSONObject dataJson=null;
	
	private JSONObject nbaJson=null;
	    @Override
    
    public void onCreate(Bundle savedInstanceState) 
    {
     
       super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        try{
        	
        lv=(ListView)findViewById(R.id.listView1);
        Bundle bundle = this.getIntent().getExtras(); 
        String text = bundle.getString("KEY_TEXT"); 
        String str = bundle.getString("KEY_Object");         
        dataJson = new JSONObject(str);
        if(null!=dataJson) 
			
       		Log.d(TAG,"datajson is =========="+dataJson);    		
        			
        nbaJson = dataJson.getJSONObject(text);
        
      
        JSONArray js=nbaJson.names();
        String[] result=new String[js.length()];
      
        for(int i=0;i<js.length();i++)
        {
        	result[i]=js.optString(i);
        	String value=nbaJson.optString(result[i]);
        	result[i]=result[i]+"  is  "+ value;
        }
          ListAdapter adapter=new ArrayAdapter<String>(
        		this,android.R.layout.simple_list_item_1 ,
        		result);
        lv.setAdapter(adapter);

    	 }catch (JSONException e){
	  
    		 e.printStackTrace();
    		 Log.d(TAG," nba is=========="+nbaJson); 
    	}	

使用了listview的控件
使用了intent 页面转换
 
不知道做游戏的是怎么做的,现在只是知道了这么点皮毛而已。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值