Android 如何解析这个json数据?并将其直接存储在数据库中,然后在listview中显示
//这是我的json数据 json///这是主json对象Android 如何解析这个json数据?并将其直接存储在数据库中,然后在listview中显示,android,json,Android,Json,//这是我的json数据 json///这是主json对象 { "cityMasterEntity": --This is array [ { "CityId": 1, "CityName": "Ahmedabad", "CreatedDate": "\/Date(1373091319697+0530)\/", "IsActive": true, "StateId": 6, "StateName": "Guja
{
"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`
]
}
解析
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
}
JSONObject jb=newjsonobject(“yourString”);
JSONArray city=jb.getJSONArray(“cityMasterEntity”);
对于(int i=0;iYash),您应该将其添加到其他问题中,因为元素太少,无法知道您正在尝试做什么以及到目前为止尝试了什么……我们甚至不知道语言(javascript?)或如何显示它(html?)。你为什么问第二个问题?你应该加入同样的问题。我的问题是:空指针异常即将到来,citymaster=newrummy.getJSONArray(CITYARRAY);///for(int i=0;icitymaster
为空。我会通过记录json来检查。为什么会被否决?不知道我没有否决它。可能是错误地做了。@yash_5111您的响应为空。所以您需要检查如何下载json。@yash_5111检查编辑它应该起作用。@Raghunandan不知道谁被否决了。但是这些都是错误的答案是有用的。
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
}
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();
}
}
}
}
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);
}
}