2014-07-11 3 views

답변

1

이는 데이터 유형에 따라 달라집니다. JSON 피드에서 eathquake 데이터 위치를 가져 오는 예제를 구성하십시오.

{ 
"id": "nc72241526", 
"type": "Feature", 
"geometry": { 
    "type": "Point", 
    "coordinates": [ 
     -122.0102, 
     37.6053, 
     6 
    ] 
}, 
"properties": { 
    "detail": "http://earthquake.usgs.gov/earthquakes/feed/v1.0/detail/nc72241526.geojson", 
    "type": "earthquake", 
    "net": "nc", 
    "tsunami": null, 
    "sources": ",nc,", 
    "title": "M 3.0 - 1km NE of Union City, California", 
    "time": 1403371328500, 
    "updated": 1403374699020, 
    "mag": 3, 
    "types": ",dyfi,focal-mechanism,general-link,geoserve,nearby-cities,origin,phase-data,scitech-link,tectonic-summary,", 
    "place": "1km NE of Union City, California", 
    "status": "AUTOMATIC", 
    "ids": ",nc72241526,", 
    "alert": null, 
    "rms": 0.17, 
    "code": "72241526", 
    "url": "http://earthquake.usgs.gov/earthquakes/eventpage/nc72241526", 
    "magType": "Md", 
    "mmi": null, 
    "cdi": 3.8, 
    "tz": -420, 
    "felt": 319, 
    "nst": 75, 
    "dmin": 0.03593261, 
    "gap": 25.2, 
    "sig": 260 
} 

}

그리고 당신이 된 JSONObject 클래스를 시작하고 관심있는 데이터 포인트로 드릴 다운 것 :

try { 
     JSONObject object = (JSONObject) new JSONTokener(JSONResponse) 
       .nextValue(); 
     JSONArray earthquakes = object.getJSONArray("features"); 

     for (int i = 0; i < earthquakes.length(); i++) { 
      JSONObject tmp = (JSONObject) earthquakes.get(i); 
      //Log.i("JSON: ",tmp.toString()); 

      JSONObject geometry = tmp.getJSONObject("geometry"); 
      JSONArray coords = geometry.getJSONArray("coordinates"); 
      JSONObject properties = tmp.getJSONObject("properties"); 

      //Log.i("Data", "Coords:"+coords.getString(0) + " "+ coords.getString(1)+"\n Place:"+properties.getString("place")+ " Mag:"+properties.getString("mag")); 

      if(coords.getString(0) != "" && coords.getString(1) != ""){ 
       result.add(new EarthQuakeRec(
         Float.parseFloat(coords.getString(1)),//Lat 
         Float.parseFloat(coords.getString(0)),//Long 
         Float.parseFloat(properties.getString("mag")),//Magnitude 
         properties.getString("place") 
        ) 
       ); 
      }   
     } 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

는 그리고 JSON 구조는 다음과 같이 보인다. Log 클래스를 사용하면 데이터 구조를 탐색하는 과정에서 문제를 해결하고 진행 상황을 파악하는 데 도움이됩니다. 내 예에서는 주석 처리가되어 있습니다.

 try { 

     // Create the Pull Parser 
     XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
     XmlPullParser xpp = factory.newPullParser(); 

     // Set the Parser's input to be the XML document in the HTTP Response 
     xpp.setInput(new InputStreamReader(response.getEntity() 
       .getContent())); 

     // Get the first Parser event and start iterating over the XML document 
     int eventType = xpp.getEventType(); 

     while (eventType != XmlPullParser.END_DOCUMENT) { 

      if (eventType == XmlPullParser.START_TAG) { 
       startTag(xpp.getName()); 
      } else if (eventType == XmlPullParser.END_TAG) { 
       endTag(xpp.getName()); 
      } else if (eventType == XmlPullParser.TEXT) { 
       text(xpp.getText()); 
      } 
      eventType = xpp.next(); 
     } 
     return mResults; 
    } catch (XmlPullParserException e) { 
     e.printStackTrace(); 
    } 

그것은 당신을 던져 많은,하지만 당신은 많은 특이성을 포기하지 않았다 :

는 XML를 들어, 다음의 조정에 뭔가를 시도 할 수 있습니다. 희망이 도움이됩니다. ;)

+0

나는 특이성의 부족에 대해 사과드립니다. 내가 끌어 내려고했던 것은 정확한 순간과 날씨 조건 (맑은 날씨, 안개가 끼는 날씨 등)에서의 기온이었습니다. – Lufval

+0

@Lufval 어떤 형식입니까? –

+0

형식이 무엇을 의미하는지 확신 할 수 없습니다 (미안, 매우 새로운). – Lufval