JsonActivity.java
package com.gusfree.json; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class JsonActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView textView= new TextView(this); //Object : {string:value,string:value,string:value...} String test1="{title:음식점,price:2000,location:강남}"; String content=""; try { JSONObject jsonObject = new JSONObject(test1); String title =jsonObject.getString("title"); String price =jsonObject.getString("price"); String location =jsonObject.getString("location"); content="title: "+title+" price: "+price+" locaction: "+location; } catch (JSONException e) { e.printStackTrace(); } textView.setText(content); setContentView(textView); } } /* <Data> * <title>음식점 </title> * <price>2000 </price> * <location>강남 </location> * </Data> * * * {"음식점",2000,강남} //Json 방식 Mobile * */
package com.gusfree.json; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class JsonActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView textView= new TextView(this); //Object : {string:value,string:value,string:value...} String test1="{title:음식점,price:2000,location:강남}"; String content=""; try { JSONObject jsonObject = new JSONObject(test1); String title =jsonObject.getString("title"); String price =jsonObject.getString("price"); String location =jsonObject.getString("location"); content="title: "+title+" price: "+price+" locaction: "+location; //JSONArray 따옴표 안에 따옴표를 표현하고 싶을 때 //\n String test2="[ \"목요일 \",\"금요일 \" ]"; JSONArray jasonArray =new JSONArray(test2); Object first = jasonArray.get(0); Object second = jasonArray.get(1); content += "\n" +first.toString() + "\n" +second.toString(); } catch (JSONException e) { e.printStackTrace(); } textView.setText(content); setContentView(textView); } } /* <Data> * <title>음식점 </title> * <price>2000 </price> * <location>강남 </location> * </Data> * * * {"음식점",2000,강남} //Json 방식 Mobile * */
'Android > 2012.04월 강좌' 카테고리의 다른 글
12일차 SocketServer 2 (Client,Server C->C) (0) | 2012.05.11 |
---|---|
11일차 SocketServer (Client,Server C->S, S->C) (0) | 2012.05.10 |
11일차 NavarOpenAPI 이용한 Dom Parser (0) | 2012.05.10 |
11일차 DOM과 SAX의 기술적 특징 (0) | 2012.05.10 |
11일차 오픈 API 사용법 (0) | 2012.05.10 |