【发布时间】:2012-01-18 09:57:20
【问题描述】:
我有一个如下的大 JSON 文件。
[{"id":0,"name":"Mrs. Tess Goyette","priceRange":{"min":86,"max":87},"requirements":["close to transport","internet","bath","gym"]},..
同样有 999 个这样的项目。我想将此数据发送到 MySQL 数据库。还 用它做一些交易。还有一个像这样的文件,其中包含 40 个酒店名称和称为住宿的数据。
我无法使用 java 解析此文件。有人可以帮忙吗?我尝试在其中创建一个 JSON 对象和数组,但失败了。enter code here
感谢任何帮助。
是的,我确实尝试过编写这样的代码。
public static void main(String[] args) {
JSONParser parser = new JSONParser();
try {
JSONParser parser = new JSONParser();
InputStream is = hoteldemo.class.getResourceAsStream("accommodation.json");
String jsonTxt = IOUtils.toString(is);
JSON json = JSONSerializer.toJSON(jsonTxt);
JSONObject json1 = (JSONObject)JSONSerializer.toJSON(jsonTxt);
JSONObject json2= json1.getJSONObject("menu");
double id = json2.getDouble("id");
String name = json2.getString("name");
int price=json2.getInt("price");
JSONArray jarray=json2.getJSONArray("attributes");
//JSONArray jarray1=json1.getJSONArray("capacity");
int a= json2.size();
for(int i=0;i<json2.size();i++)
{
System.out.println("The id is "+ json2.getDouble("id"));
System.out.println("The name is "+ json2.getString("name"));
System.out.println("The price is "+ json2.getInt("price"));
System.out.println("the size of object is" + a);
}
/* for(int i=0;i<jarray.size();i++)
{
System.out.println("This is ameneities id is " + jarray.getString(i));
}
for(int j=0;j<jarray1.size();j++)
{
System.out.println("This is ameneities id is " + jarray1.getString(j));
}*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
但它只给了我一个对象,比如只有第一个 id 和下一个 id 在 for 循环中不可访问。
【问题讨论】:
-
请向我们展示您的代码并告诉我们what you have already tried。
-
我做过这样的事情。
-
“我做过这样的事情。” 我将其提名为当天最无意义(因此也毫无意义)的评论。 1)准确地告诉我们,而不是“类似”你尝试了什么。 2)“这个”什么?当我们问您尝试了什么时,我们的意思是“什么代码?”。虽然“this”是 Java 关键字,但它不是代码。
-
我猜,他在他的问题中添加了代码。但是是的,评论确实具有欺骗性;)
-
您使用哪个库来处理 JSON? Google GSON, json.org?
标签: java json html-parsing