//这是my__json__数据 json ////这是主要的json对象
{
"cityMasterEntity":
--This is array
[
{
"CityId": 1,
"CityName": "Ahmedabad",
"CreatedDate": "\/Date(1373091319697+0530)\/",
"IsActive": true,
"StateId": 6,
"StateName": "Gujarat\u000d\u000a",
"UpdatedDate": null
},
{
"CityId": 3,
"CityName": "Rajkot",
"CreatedDate": "\/Date(1373091319697+0530)\/",
"IsActive": true,
"StateId": 6,
"StateName": "Gujarat\u000d\u000a",
"UpdatedDate": null
},
{
"CityId": 2,
"CityName": "Surat",
"CreatedDate": "\/Date(1373091319697+0530)\/",
"IsActive": true,
"StateId": 6,
"StateName": "Gujarat\u000d\u000a",
"UpdatedDate": null
}`enter code here`
]
}
答案 0 :(得分:1)
{ // json object node
"cityMasterEntity":[ // json array
{ // json object node
"CityId": 1
解析
JSONObject jb = new JSONObject("yourString");
JSONArray city = jb.getJSONArray("cityMasterEntity");
for(int i=0;i<city.length();i++)
{
JSONObject jb1 =(JSONObject) city.getJSONObject(i);
int id = jb1.getInt("CityId");
String name = jb1.getString("CityName");
...// similarl for name and others
}
要将其存储在数据库中,您可以使用sqlite。要显示ca,请使用列表视图。
编辑:
class TheTask extends AsyncTask<Void,Void,String>
{
@Override
protected String doInBackground(Void... arg0) {
String _response= null;
try
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet("http://www.clubrummy.in/MobileWCF/RummyService/GetMasterDetail");
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
_response=EntityUtils.toString(resEntity); // content will be consume only once
Log.i(".......",_response);
}catch(Exception e)
{
}
return _response;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if(result!=null)
{
try
{
JSONObject jb = new JSONObject(result);
JSONArray city = jb.getJSONArray("cityMasterEntity");
for(int i=0;i<city.length();i++)
{
JSONObject jb1 =(JSONObject) city.getJSONObject(i);
int id = jb1.getInt("CityId");
String name = jb1.getString("CityName");
Log.i("Name is",""+name);
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}
}
编辑2:
实际上,您可以在doInbackground本身中解析数据库中的json存储数据。在onPostExecute中更新ui
class TheTask extends AsyncTask<Void,Void,Void>
{
@Override
protected Void doInBackground(Void... arg0) {
String _response= null;
try
{
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpGet request = new HttpGet("http://www.clubrummy.in/MobileWCF/RummyService/GetMasterDetail");
HttpResponse response = httpclient.execute(request);
HttpEntity resEntity = response.getEntity();
_response=EntityUtils.toString(resEntity); // content will be consume only once
Log.i(".......",_response);
if(_response!=null)
{
JSONObject jb = new JSONObject(_response);
JSONArray city = jb.getJSONArray("cityMasterEntity");
for(int i=0;i<city.length();i++)
{
JSONObject jb1 =(JSONObject) city.getJSONObject(i);
int id = jb1.getInt("CityId");
String name = jb1.getString("CityName");
Log.i("Name is",""+name);
}
}
}catch(Exception e)
{
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
}
此外,如果从数据库加载数据需要时间,请使用加载器。