多语言展示
当前在线:1077今日阅读:27今日分享:41

json中如何截取字符串

今天有网友问我 json中如何截取字符串 ,小编在网上查了些资料,再根据个人的经验总结。希望能帮助到大家。
工具/原料
1

电脑或者手机

2

各种编辑工具

方法/步骤
1

注意定义变量:int endIndex = result.lastIndexOf(')');int startIndex = result.indexOf('(')+1;String result1 = result.substring(startIndex, endIndex);Log.e('result1', ''result1)。

2

源码:Matcher matcher=Pattern.compile(regex).matcher(parse),while(matcher.find(),String ret=matcher.group(1)System.out.println(ret)。

3

代码:private TextView  textView = null;private Object json;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);textView = (TextView)this.findViewById(R.id.textview); new Thread(){,public void run()。

4

注意格式:JSONObject JSONObject = new JSONObject(json);if(JSONObject!=null){if(!JSONObject.isNull('mobile')){System.out.printl(JSONObject.optStrig('mobile'));}if(!JSONObject.isNull('province')){System.out.println(JSONObject.optString('province'))。

注意事项
1

大家要注意编辑时候的格式

2

大家要注意代码的顺序

推荐信息