2015-10-30 46 views
1
//Open url and fetch JSON data 
String s = "MY_URL_HERE"; 
URL url = new URL(s); 
Scanner scan = new Scanner(url.openStream()); 
String str = new String(); 
while (scan.hasNext()) 
{ 
    str += scan.nextLine(); 
} 
scan.close(); 

System.out.println(str); 

海峽將打印像一個字符串:如何從JSON字符串一個特定的值

{"coord":{"lon":-80.25,"lat":43.55},"weather":[{"id":800,"main":"Clear","description":"Sky is Clear","icon"...ect 

我使用json_simple-1.1.jar

如何實際使用此字符串來提取我選擇的價值?如果我想拉出「描述」或「天氣」。

我曾嘗試與代碼片斷:

https://code.google.com/p/json-simple/wiki/DecodingExamples

這些不爲我工作,我得到一個錯誤

org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray 

回答

3

您的字符串不是螞蟻JSON數組表示,但JSON對象本身。那是什麼錯誤org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray描述。在您的鏈接實施例描述的陣列,並且該陣列的每個元素是一個對象:

考慮Jackson libray這是很方便從JSON

下面的Java對象轉換成/代表的陣列,並且每個元件該數組的一個目的是:

[ 
    { 
     color: "red", 
     value: "#f00" 
    }, 
    { 
     color: "green", 
     value: "#0f0" 
    } 
] 

or 

[ 
    { 
     "color": "red", 
     "value": "#f00" 
    }, 
    { 
     "color": "green", 
     "value": "#0f0" 
    } 
] 

下面表示對象

{ 
    color: "red", 
    value: "#f00" 
} 

or 

{ 
    "color": "red", 
    "value": "#f00" 
} 

使用傑克遜庫JSON字符串

代碼樣本的不同表現見here

import java.io.IOException; 

import com.fasterxml.jackson.core.JsonParseException; 
import com.fasterxml.jackson.databind.JsonMappingException; 
import com.fasterxml.jackson.databind.ObjectMapper; 


public class TestString { 

    public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException { 

     String s="{\"coord\":{\"lon\":-80.25,\"lat\":43.55},\"weather\":[{\"id\":800,\"main\":\"Clear\",\"description\":\"Sky is Clear\"}]}"; 
     //String s="[{\"coord\":\"test\",\"lon\":-80.25}]"; 
     ObjectMapper mapper = new ObjectMapper(); 
     Object obj = mapper.readValue(s, Object.class); 
     System.out.println("terst"+ obj); 
    } 

} 
+0

不錯,我也記得。 – Acewin

+0

也可以請添加,如果它是一個對象列表將會發生 – Acewin

+0

JSONObject.get(「coord」)我不確定你是什麼意思。 我應該先創建一個對象 JSONObject obj = new JSONObject(); ?? –

0

的人在未來的看着這個,

使用json_simple-1.1.jar

String s = "YOUR_URL_HERE"; 
    URL url = new URL(s); 
    Scanner scan = new Scanner(url.openStream()); 
    String str = new String(); 
    while (scan.hasNext()) 
    { 
     str += scan.nextLine(); 
    } 
    scan.close(); 

str現在包含S(在我的情況)含有較多的對象的對象(**會編輯的時候,當我敢肯定,這就是我的想法)

現在使用此代碼,並使用http://codebeautify.org/jsonviewer

JSONObject obj = (JSONObject) JSONValue.parse(str); 

    String info = obj.get("object_name_you_wish_to_call").toString(); 

我看到上面的代碼將輸出另一個Object,其中包含該特定對象中的所有信息。在這種情況下,當我計算出如何檢索特定值時,我將再次編輯該對象中的所有對象。「object_name_you_wish_to_call」

0

可以使用這樣做:

  1. 模式
  2. 匹配器
  3. ObjectMapper
  4. JsonNode

Java類:

private Pattern pattern; 
    private Matcher matcher; 

    private static final String PATTERN = "description"; 

    private static String StringFinal = null; 

    public void printAll(String str) { 

     pattern = Pattern.compile(PATTERN); 
     String finalPattern = null; 

     ObjectMapper mapper = new ObjectMapper(); 
     JsonNode node = mapper.readTree(str); 
     Iterator<String> fieldNames = node.fieldNames(); 


     while(fieldNames.hasNext()){ 
      String fieldName = fieldNames.next(); 
      JsonNode fieldValue = node.get(fieldName); 

      if (fieldValue.isObject()) { 
       printAll(fieldValue); 
      } 
      else { 
       String value = fieldValue.asText(); 
       matcher = pattern.matcher(value); 
       if(matcher.matches()){ 
        StringFinal = value; 
        break; 
       } 
      } 
     } 

     System.out.println(StringFinal); 
    } 
相關問題