我怎样才能解析没有任何阵列只是一个单一的对象 [英] How can i parse without any array just a single object
本文介绍了我怎样才能解析没有任何阵列只是一个单一的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在Android开发领域的新
如何分析眼前这个对象。
{
USER_ID:21,
名字:,
姓:,
电子邮件:9654008793,
isd_ code:91,
手机:9654008793,
性别:,
DOB:0000-00-00,
形象:空,
状态:0
verification_key:4518
}
解决方案
只要按照下面的教程,这将有助于你它帮助我...:)
http://www.androidhive.info/2012/01 / Android的JSON的解析教程/
在项目中创建一个类文件,并将它命名为JSONParser.java。解析器类有一个方法,这将使得HTTP请求来获取JSON数据并返回一个JSONObject。
公共类JSONParser {静态InputStream为= NULL;
静态的JSONObject jObj = NULL;
静态JSON字符串=;//构造
公共JSONParser(){}公众的JSONObject getJSONFromUrl(字符串URL){ //使HTTP请求
尝试{
// defaultHttpClient
DefaultHttpClient的HttpClient =新DefaultHttpClient();
HttpPost httpPost =新HttpPost(URL); HTT presponse HTT presponse = httpClient.execute(httpPost);
HttpEntity httpEntity = HTT presponse.getEntity();
是= httpEntity.getContent(); }赶上(UnsupportedEncodingException五){
e.printStackTrace();
}赶上(ClientProtocolException E){
e.printStackTrace();
}赶上(IOException异常五){
e.printStackTrace();
} 尝试{
读者的BufferedReader =新的BufferedReader(新的InputStreamReader(
是,ISO-8859-1),8);
StringBuilder的SB =新的StringBuilder();
串线= NULL;
而((行= reader.readLine())!= NULL){
sb.append(行+\\ n);
}
is.close();
JSON = sb.toString();
}赶上(例外五){
Log.e(缓冲区错误,错误转换结果+ e.toString());
} //尝试分析字符串到一个JSON对象
尝试{
jObj =新的JSONObject(JSON);
}赶上(JSONException E){
Log.e(JSON解析器,错误分析数据+ e.toString());
} //返回JSON字符串
返回jObj;}
}
解析JSON数据并更新到ListView控件
公共类AndroidJSONParsingActivity扩展ListActivity {// URL使请求
私有静态字符串URL =http://api.androidhive.info/contacts/;// JSON节点名称
私有静态最后弦乐TAG_CONTACTS =接触;
私有静态最后弦乐TAG_ID =ID;
私有静态最后弦乐TAG_NAME =名;
私有静态最后弦乐TAG_EMAIL =电子邮件;
私有静态最后弦乐TAG_ADDRESS =地址;
私有静态最后弦乐TAG_GENDER =性别;
私有静态最后弦乐TAG_PHONE =手机;
私有静态最后弦乐TAG_PHONE_MOBILE =移动;
私有静态最后弦乐TAG_PHONE_HOME =家;
私有静态最后弦乐TAG_PHONE_OFFICE =办公室;//接触JSONArray
JSONArray接触= NULL;@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main); //为哈希映射的ListView
ArrayList的<&HashMap的LT;字符串,字符串>> contactList =新的ArrayList<&HashMap的LT;字符串,字符串>>(); //创建JSON解析器实例
JSONParser jParser =新JSONParser(); // URL从获取JSON字符串
JSONObject的JSON = jParser.getJSONFromUrl(URL); 尝试{
//获取联系人的数组
接触= json.getJSONArray(TAG_CONTACTS); //通过所有联系人循环
的for(int i = 0; I< contacts.length();我++){
JSONObject的C = contacts.getJSONObject(I) //存储在变量中的每个JSON项目
字符串ID = c.getString(TAG_ID);
字符串名称= c.getString(TAG_NAME);
字符串email = c.getString(TAG_EMAIL);
字符串的地址= c.getString(TAG_ADDRESS);
字符串性别= c.getString(TAG_GENDER); //电话号码是阿恩JSON对象
JSONObject的手机= c.getJSONObject(TAG_PHONE);
字符串移动= phone.getString(TAG_PHONE_MOBILE);
字符串家= phone.getString(TAG_PHONE_HOME);
串办公室= phone.getString(TAG_PHONE_OFFICE); //创建新的HashMap
HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>(); //将每个子节点的HashMap键=>值
map.put(TAG_ID,身份证);
map.put(TAG_NAME,名);
map.put(TAG_EMAIL,电子邮件);
map.put(TAG_PHONE_MOBILE,手机); //添加HashList到ArrayList的
contactList.add(地图);
}
}赶上(JSONException E){
e.printStackTrace();
}
/ **
*更新解析JSON数据到ListView控件
* * /
ListAdapter适配器=新SimpleAdapter(这一点,contactList,
R.layout.list_item,
新的String [] {TAG_NAME,TAG_EMAIL,TAG_PHONE_MOBILE},新的INT [] {
R.id.name,R.id.email,R.id.mobile}); setListAdapter(适配器); //选择单个ListView项
ListView控件LV = getListView(); //在选择单列表项启动新画面
lv.setOnItemClickListener(新OnItemClickListener(){ @覆盖
公共无效onItemClick(适配器视图<>母公司,观景,
INT位置,长的id){
//从选定的ListItem得到的值
字符串名称=((的TextView)view.findViewById(R.id.name))的getText()的toString()。
串成本=((的TextView)view.findViewById(R.id.email))的getText()的toString()。
字符串描述=((的TextView)view.findViewById(R.id.mobile))的getText()的toString()。 //开始新意图
在意向=新意图(getApplicationContext(),SingleMenuItemActivity.class);
in.putExtra(TAG_NAME,名);
in.putExtra(TAG_EMAIL,成本);
in.putExtra(TAG_PHONE_MOBILE,说明);
startActivity(在);
}
});
}}
I am new in the field of android development.
How to parse just this object.
{
"user_id":21,
"firstname":"",
"lastname":"",
"email":"9654008793",
"isd_code":"91",
"mobile":"9654008793",
"gender":"",
"dob":"0000-00-00",
"image":null,
"status":"0",
"verification_key":"4518"
}
解决方案
Just follow following tutorial this will help you it helped me...:)
http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
In your project create a class file and name it as JSONParser.java. The parser class has a method which will make http request to get JSON data and returns a JSONObject.
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
// constructor
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Parsing JSON data and updating into ListView
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
private static String url = "http://api.androidhive.info/contacts/";
// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
// contacts JSONArray
JSONArray contacts = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>();
// Creating JSON Parser instance
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
contacts = json.getJSONArray(TAG_CONTACTS);
// looping through All Contacts
for(int i = 0; i < contacts.length(); i++){
JSONObject c = contacts.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone number is agin JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NAME, name);
map.put(TAG_EMAIL, email);
map.put(TAG_PHONE_MOBILE, mobile);
// adding HashList to ArrayList
contactList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, contactList,
R.layout.list_item,
new String[] { TAG_NAME, TAG_EMAIL, TAG_PHONE_MOBILE }, new int[] {
R.id.name, R.id.email, R.id.mobile });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_EMAIL, cost);
in.putExtra(TAG_PHONE_MOBILE, description);
startActivity(in);
}
});
}
}
这篇关于我怎样才能解析没有任何阵列只是一个单一的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文