我有这样的json:
[ { "id": "c200", "name": "Ravi Tamada", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone" : "+91 0000000000" },
{ "id": "c201", "name": "Hero", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone" : "+90 0000000000" }]
我想知道如何从Web服务中获取json。我知道一些关于这方面的教程,但json总是那样:
{ "contacts": [ { "id": "c200", "name": "Ravi Tamada", "email": "ravi@gmail.com", "address": "xx-xx-xxxx,x - street, x-country", "gender" : "male", "phone" : "00 000000" },
{ "id": "c201", "name": "Johnny Depp", "email": "johnny_depp@gmail.com", "address": "xx-xx-xxxx,x - street, x - country", "gender" : "male", "phone" : "00 000000", } ] }
但我不喜欢在“联系人”中获取数据,我只想在“联系人”中获取数据,这意味着“id”,“name”....谁能帮帮我,拜托!
答案 0 :(得分:0)
假设您使用的是http://www.androidhive.info/2012/01/android-json-parsing-tutorial/
之类的教程试试这个:
其中JSONObject json = jParser.getJSONFromUrl(url);
更改为JSONArray jsonArray = jParser.getJSONFromUrl("url");
您还必须将getJSONFromUrl(String url)
的返回类型改为JSONArray
答案 1 :(得分:0)
您可以使用ArrayList<>没有解析Json
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://10.250.11.139/AirlineWebService/FlightsService.asmx";
private final String SOAP_ACTION = "http://tempuri.org/GetAllFlights";
public String METHOD_NAME = "";
public static ArrayList<Product> getAllProducts(String category,int mode)
{
ArrayList<Product> listProducts = new ArrayList<Product>();
String METHOD_NAME="GetAllProducts";
Log.d("GetAllProducts", "Inside GetAllProducts function");
try
{
SoapObject webRequest = new SoapObject(NAMESPACE, METHOD_NAME);
PropertyInfo categ=new PropertyInfo();
categ.setName("category");
categ.setValue(category);
categ.setType(String.class);
webRequest.addProperty(categ);
PropertyInfo pswd=new PropertyInfo();
pswd.setName("mode");
pswd.setValue(mode);
pswd.setType(Integer.class);
webRequest.addProperty(pswd);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(webRequest);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
androidHttpTransport.call(SOAP_ACTION + METHOD_NAME, envelope);
Log.d("GetAllProducts - SOAP ACTION", SOAP_ACTION + METHOD_NAME);
SoapObject response = (SoapObject)envelope.getResponse();
int count=response.getPropertyCount();
Log.d("GetAllCategories- Response", response.toString());
for(int i=0;i<count; ++i)
{
SoapObject p= (SoapObject) response.getProperty(i);
listProducts.add(new Product(Integer.parseInt(p.getProperty(0).toString()),p.getProperty(1).toString(), p.getProperty(2).toString(), p.getProperty(3).toString(), Double.parseDouble(p.getProperty(4).toString()), Double.parseDouble(p.getProperty(5).toString()),p.getProperty(6).toString()));
Log.d("GetAllCategories- Response", p.toString());
}
return listProducts;
}catch (Exception e) {
// TODO: handle exception
return listProducts;
}
}
答案 2 :(得分:0)
用户JsonArray作为基本元素。
像:
JsonArray jArray = new JsonArray(jsonString);
for(int i=0;i<jArray.size();i++){
// Here you can get your json object
JsonObject jObj = jArray.getJSONObject(i);
// use this json object: jObj
String id = jObj.getString("id");
}