我想在我的程序中使用来自雅虎的财务数据,它已经有效了。我得到完整的JSON内容,我可以显示它。但现在我想提取价格为 int 。
ID ..(some other columns).. food category
1 apple vegetable
2 sausage sausage
3 tomato vegetable
4 cabbage vegetable
5 vodka vodka
修改 这是来自雅虎的JSON数据
public class Main {
public static void main (String[]args) throws IOException {
String sURL = "http://finance.yahoo.com/webservice/v1/symbols/googl/quote?format=json"; //just a string
// Connect to the URL using java's native library
URL url = new URL(sURL);
HttpURLConnection request = (HttpURLConnection) url.openConnection();
request.connect();
// Convert to a JSON object to print data
JsonParser jp = new JsonParser(); //from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //convert the input stream to a json element
JsonObject rootobj = root.getAsJsonObject(); //may be an array, may be an object.
System.out.print(rootobj);
}
}
编辑2
我改变了我的代码
{
"list" : {
"meta" : {
"type" : "resource-list",
"start" : 0,
"count" : 1
},
"resources" : [
{
"resource" : {
"classname" : "Quote",
"fields" : {
"name" : "Google Inc.",
"price" : "554.520020",
"symbol" : "GOOGL",
"ts" : "1432324800",
"type" : "equity",
"utctime" : "2015-05-22T20:00:00+0000",
"volume" : "1213288"
}
}
}
]
}
}
现在我已经得到了这个
JsonParser jp = new JsonParser(); //from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //convert the input stream to a json element
JsonObject obj = root.getAsJsonObject();
JsonObject result = obj.get("list").getAsJsonObject();
String result2 = result.get("resources").toString();
System.out.print(result2);
我现在如何获得“价格”?
编辑3
好的,我现在得到它,它可以工作,我的价格只有双倍,但这是解决这个任务的聪明方法吗?
[{"resource":{"classname":"Quote","fields":{"name":"Google Inc.","price":"554.520020","symbol":"GOOGL","ts":"1432324800","type":"equity","utctime":"2015-05-22T20:00:00+0000","volume":"1213288"}}}]
答案 0 :(得分:0)
你应该到达“fields”对象来提取“name”,“price”等。
org.json
库易于使用。以下示例代码:您的响应为字符串:
JSONObject obj1 = new JSONObject(response);
JSONArray arr = obj1.getJSONObject("list").getJSONArray("resources"); //GETS RESOURCES ARRAY
for (int i = 0; i < arr.length(); i++)
{
String resource = arr.getJSONObject(i).toString();
JSONObject obj2 = new JSONObject(resource);
String resourceObject = obj2.getJSONObject("resource").toString(); //RESOURCE OBJECT
JSONObject obj3 = new JSONObject(resourceObject);
String name = obj3.getJSONObject("fields").getString("name"); //REACHED THE FIELDS
float price = (float)obj3.getJSONObject("fields").getDouble("price");
System.out.println(name);
System.out.println(price);
}
答案 1 :(得分:0)
他已经在使用gson。
如果你想继续使用gson并且之前知道结构,你可以创建存储数据的类。
class GoogleRequest{
private GoogleList list;
public GoogleList getList() {
return list;
}
public void setList(GoogleList list) {
this.list = list;
}
}
// class for list
class GoogleList{
private Meta meta;
private List<Resources> resources;
public List<Resources> getResources() {
return resources;
}
public void setResources(List<Resources> resources) {
this.resources = resources;
}
public Meta getMeta() {
return meta;
}
public void setMeta(Meta meta) {
this.meta = meta;
}
}
// create other classes here like the Resources class
JsonParser jp = new JsonParser(); // from gson
JsonElement root = jp.parse(new InputStreamReader((InputStream)request.getContent()));
GoogleRequest list = new Gson().fromJson(root,GoogleRequest.class);
GoogleRequest应该包含List对象和Meta对象。 gson将内省并设置属性。如果没有内省的话,gson会将属性设置为null。所以你可以使用。
if( list.getResources() != null ){
// list is here
}else{
// do some other code and parse diffrent json
}
如果您不知道它是数组还是对象,请创建不同的类来为您处理它。只需使用新的Gson()。fromJson();
解析数据现在请记住,您需要正确的工作属性。假设你在java中有这个json
String json = "{\"price\" : \"554.520020\"}";
然后价格必须是双倍或双倍。如果你使用Double,你可以检查
if( obj.getPrice() != null ){
System.out.println( obj.getPrice().intValue() );
}
注意:如果将double转换为int
,则会失去精度