现有JSON格式数据的数组 我希望将这个格式的菜单转换成对象数组 在线等待解决
package org.json;import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import net.sf.json.JSONString;
import net.sf.json.*;
import org.json.*; public class test { public static void main(String[] args) {
String jsonStr = "{\"item\":[{\"品牌编码\":\"999999\",\"品牌名称\":\"keluoyi\",\"积分\":\"0.0100\"},{\"品牌编码\":\"136\",\"品牌名称\":\"克洛伊\",\"积分\":\"1.0000\"},{\"品牌编码\":\"135\",\"品牌名称\":\"酷美娇套装\",\"积分\":\"1.0000\"}]}";
JSONObject o = JSONObject.fromObject(jsonStr);
JSONArray jArray = o.getJSONArray("item");
System.out.println(jArray);实际输出结果为:[{"品牌编码":"999999","品牌名称":"keluoyi","积分":"0.0100"},{"品牌编码":"136","品牌名称":"克洛伊","积分":"1.0000"},{"品牌编码":"135","品牌名称":"酷美娇套装","积分":"1.0000"}]
目标输出结果为: 品牌编码:999999,品牌名称:keluoyi,积分:0.0100 ..........本人初学java..就遇到需要解析json的问题了..希望大家帮帮菜鸟吧...
package org.json;import net.sf.json.JSONObject;
import net.sf.json.JSONArray;
import net.sf.json.JSONString;
import net.sf.json.*;
import org.json.*; public class test { public static void main(String[] args) {
String jsonStr = "{\"item\":[{\"品牌编码\":\"999999\",\"品牌名称\":\"keluoyi\",\"积分\":\"0.0100\"},{\"品牌编码\":\"136\",\"品牌名称\":\"克洛伊\",\"积分\":\"1.0000\"},{\"品牌编码\":\"135\",\"品牌名称\":\"酷美娇套装\",\"积分\":\"1.0000\"}]}";
JSONObject o = JSONObject.fromObject(jsonStr);
JSONArray jArray = o.getJSONArray("item");
System.out.println(jArray);实际输出结果为:[{"品牌编码":"999999","品牌名称":"keluoyi","积分":"0.0100"},{"品牌编码":"136","品牌名称":"克洛伊","积分":"1.0000"},{"品牌编码":"135","品牌名称":"酷美娇套装","积分":"1.0000"}]
目标输出结果为: 品牌编码:999999,品牌名称:keluoyi,积分:0.0100 ..........本人初学java..就遇到需要解析json的问题了..希望大家帮帮菜鸟吧...
for (int i = 0; i < jArray.length(); i++) {
JSONObject jo = (JSONObject) jArray.get(i);
System.out.println(jo.get("品牌编码"));
}
import java.util.Iterator;import net.sf.json.JSONArray;
import net.sf.json.JSONObject;public class Test { public static void main(String[] args) { String jsonStr = "{\"item\":[{\"品牌编码\":\"999999\",\"品牌名称\":\"keluoyi\",\"积分\":\"0.0100\"},{\"品牌编码\":\"136\",\"品牌名称\":\"克洛伊\",\"积分\":\"1.0000\"},{\"品牌编码\":\"135\",\"品牌名称\":\"酷美娇套装\",\"积分\":\"1.0000\"}]}";
String str="";
JSONObject o = JSONObject.fromObject(jsonStr);
JSONArray jArray = o.getJSONArray("item");
for(Iterator it=jArray.iterator();it.hasNext();){
JSONObject s=(JSONObject)it.next();
//将中括号和引号替换成空
str=s.toString().replace("\"", "").replace("{", "").replace("}", "");
System.out.println(str);
}
}
}
品牌编码:999999,品牌名称:keluoyi,积分:0.0100
品牌编码:136,品牌名称:克洛伊,积分:1.0000
品牌编码:135,品牌名称:酷美娇套装,积分:1.0000