一、解析json
public static void xx()
{
String str
= "{'status':200,'msg':'ok'," +
"'data':[{'day':1372521600,'mile':105000,'len':4,'item_list':[{'maintain_name':'燃油滤清器','value':'23328000','value_name':'9个月'}," +
"{'maintain_name':'空气滤清器','value':'23328000','value_name':'9个月'},{'maintain_name':'机油滤清器','value':'23328000','value_name':'9个月'}," +
"{'maintain_name':'发动机机油','value':'23328000','value_name':'9个月'}]}]}";
JSONObject jsonObject = JSONObject.fromObject(str);
String name = jsonObject.getString("data");
System.out.println(name);
JSONArray jsonArray = JSONArray.fromObject(name);
System.out.println(jsonArray.get(0));
JSONObject jsonObject2 = (JSONObject) jsonArray.get(0);
System.out.println(jsonObject2.getString("day"));
// String ss = jsonObject2.getString("day");
// System.out.println(ss);
}
二、properties文件解析
package com.weike.pay.common;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
/**
* Function 返回异常信息
* User: zhangzhuo
* Date: 12-12-17
* Time: 下午5:22
*/
public class InitDetailProperty {
private static InitDetailProperty InitDetailProperty = new InitDetailProperty();
private HashMap exceptionMap = new HashMap();
private Properties properties;
public static InitDetailProperty getInstance() {
if (InitDetailProperty == null) {
InitDetailProperty = new InitDetailProperty();
}
return InitDetailProperty;
}
private InitDetailProperty()
{
init();
}
private Properties loadProperties() throws IOException, FileNotFoundException {
properties = new Properties();
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("exception.properties");
properties.load(in);
return properties;
}
public void init(){
try {
properties = loadProperties();
initConfig(properties);
} catch (Exception e) {
e.printStackTrace();
}
}
private void initConfig(Properties properties) {
Enumeration names = properties.propertyNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
exceptionMap.put(name, properties.get(name));
}
}
public HashMap getExceptionMap() {
return exceptionMap;
}
public void setExceptionMap(HashMap exceptionMap) {
this.exceptionMap = exceptionMap;
}
}
本文介绍了如何使用Java解析JSON字符串及properties配置文件的方法。通过示例代码详细展示了从JSON字符串中提取特定字段的过程,并提供了加载并读取properties文件的具体实现。
36万+

被折叠的 条评论
为什么被折叠?



