package com.animee.day12.json_text; import android.content.Context; import android.content.res.AssetManager; import android.util.Log; import com.alibaba.fastjson.JSON; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; /** * Created by Administrator on 2018/1/9. */ public class ParseJsonTest { public static void parseJson(Context context){ ByteArrayOutputStream baos = new ByteArrayOutputStream(); AssetManager assetManager = context.getAssets(); try { InputStream is = assetManager.open("json.txt"); byte[]buf = new byte[1024]; int hasRead = 0; while((hasRead = is.read(buf))!=-1){ baos.write(buf,0,hasRead); } String msg = baos.toString(); InfoBean infoBean = JSON.parseObject(msg, InfoBean.class); List<InfoBean.ParamzBean.FeedsBean> feeds = infoBean.getParamz().getFeeds(); // /sdcard/Android/data/你的包名/catch/ File cacheDir = context.getExternalCacheDir(); File file = new File(cacheDir,"subject.txt"); FileOutputStream fos = new FileOutputStream(file,true); for (int i = 0; i < feeds.size(); i++) { String subject = feeds.get(i).getData().getSubject(); Log.i("test", "parseJson: "+subject); fos.write((subject+"\n").getBytes()); fos.flush(); } } catch (IOException e) { e.printStackTrace(); } } }
fastjson解析并存储文件
最新推荐文章于 2024-03-13 19:02:26 发布