java 读取解析json以及接sdk注意事项

参考

接sdk注意事项以及本地文件读取

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class HelloWorld {
	
	public static void main(String[] args) {
		
		String context = new Util().ReadFile("res/countries.json");

		JSONArray jsonArray = JSONArray.fromObject(context);
		JSONObject object = JSONObject.fromObject(jsonArray.get(0));
		
		JSONObject detail = null;
		try {
			detail = object.getJSONObject("BRA");
		} catch (Exception e) {
			System.out.println("not found!!!");
			return;
		} finally{
			if(detail != null)
			{
				JSONArray price = detail.getJSONArray("price");
				String currencyCode = detail.getString("currencyCode");
				System.out.println(currencyCode);
				System.out.println(price);
			}
		}		
	}
}

输出:
BRA_c
[1,2,3]

json内容:

[
{
	"BRA":{
		"price":[1,2,3],
		"currency":"BRA_c"
		},
	"BRB":{
		"price":[4,5,6],
		"currency":"BRB_c"
	},
	"BRC":{
		"price":[7,8,9],
		"currency":"BRC_c"
	},
}
]



util

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class Util {
	public String ReadFile(String Path){
		BufferedReader reader = null;
		String laststr = "";
		try{
			FileInputStream fileInputStream = new FileInputStream(Path);
			InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
			reader = new BufferedReader(inputStreamReader);
			String tempString = null;
			while((tempString = reader.readLine()) != null){
				laststr += tempString;
			}
			reader.close();
		}catch(IOException e){
			e.printStackTrace();
		}finally{
			if(reader != null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return laststr;
	}
}

相关类库[上传资源]以及json文件

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

更新: 上面那套是别人包装过的? 但是在android中使用会和原生的冲突。。所以只能用原生的或别人推荐的GSon。


1.不是直接把jar复制到libs下或者直接添加jar到库中就可以。 一般要把提供给你的库 包【导入进来,作为库的工程被其他工程所引用】,但是有的时候能正确引用,有的时候不行,换个目录试试看

2.在res目录下新建文件夹 raw,然后把文件复制到该目录下,就可用通过
	R.raw.countries 来访问。  貌似是保持原始的数据而不是转为二进制之类的

3.读取文件。
	用法1:
		public String ReadFile(String path){
			BufferedReader reader = null;
			String laststr = "";
			try{
				FileInputStream fileInputStream = new FileInputStream(path);
				InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
				reader = new BufferedReader(inputStreamReader);
				String tempString = null;
				while((tempString = reader.readLine()) != null){
					laststr += tempString;
				}
				reader.close();
			}catch(IOException e){
				e.printStackTrace();
			}finally{
				if(reader != null){
					try {
						reader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return laststr;
		}

	用法2:
		public String ReadFile2(InputStream fileInputStream){
			BufferedReader reader = null;
			String laststr = "";
			try{
				InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
				reader = new BufferedReader(inputStreamReader);
				String tempString = null;
				while((tempString = reader.readLine()) != null){
					laststr += tempString;
				}
				reader.close();
			}catch(IOException e){
				e.printStackTrace();
			}finally{
				if(reader != null){
					try {
						reader.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return laststr;
		}

	按正常应该是用法1就是可以了,但是实际中就是读不出数据,后改成第二种用法:
		private void initJsonData(){
			InputStream inputStream = getResources().openRawResource(R.raw.countries);
			String context = new Util().ReadFile2(inputStream);
			JSONArray arr = null;
			try {
				arr = new JSONArray(context);
				jsonData = arr.getJSONObject(0);
			} catch (Exception e) {
			}
		}	

		如果json数据有,但是读出来有问题或者异常,可以改json为简单的试试看,有时候是汉字的空格之类的	 
		[
			{
				"name":"joe"
			}
		]



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值