我是Android新手,我无法从下面的JSON代码中检索数据。请回复一些建议。
Json代码: -
{"category":{"name":["Spa","Salon","Makeup"],"brand_title":["Aura Thai Spa- Greater Kailash 1",.....]}}
Android代码:
public class Salon extends AppCompatActivity {
String url="my_url";
String catname,brandname;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_salon);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
sname=(TextView)findViewById(R.id.person_name);
bname=(TextView)findViewById(R.id.person_age);
new MyAsyncTask().execute(url);
}
class MyAsyncTask extends AsyncTask<String, String, JSONObject> {
URLConnection urlConnection;
String result = "";
JSONObject jsonRootObject;
@Override
protected JSONObject doInBackground(String... params) {
try {
URL url_data = new URL(url);
urlConnection = url_data.openConnection();
urlConnection.connect();
// Convert response to string using String Builder
InputStream inputStream = urlConnection.getInputStream();
BufferedReader bReader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
StringBuilder sBuilder = new StringBuilder();
String line;
while ((line = bReader.readLine()) != null) {
sBuilder.append(line + "\n");
}
inputStream.close();
result = sBuilder.toString();
}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
//Log.e("StringBuilding & BufferedReader", "Error converting result " + e.toString());
}
try{
jsonRootObject = new JSONObject(result);
}catch(JSONException e){
e.printStackTrace();
}
return jsonRootObject;
}
protected void onPostExecute(JSONObject result){
try {
//Iterate the jsonArray and print the info of JSONObjects
JSONArray jsonArray = jsonRootObject.getJSONArray("name");
for(int i=0; i < jsonArray.length(); i++) {
JSONObject name_0 = jsonArray.getJSONObject(i);
catname = name_0.getString("").toString();
}
/* JSONArray jsonArray2 = jsonRootObject.getJSONArray("brand_name");
JSONObject brand = jsonArray2.getJSONObject(0);
brandname = brand.getString("brand_title[0]");*/
//loat salary = Float.parseFloat(jsonObject.optString("salary").toString());
sname.setText(catname);
//bname.setText(brandname);
} catch (JSONException e) {e.printStackTrace();}
}
}
}
答案 0 :(得分:0)
尝试更换:
JSONArray jsonArray = jsonRootObject.getJSONArray("name");
使用:
JSONArray jsonArray = jsonRootObject.getJSONObject("category").getJSONArray("name");
答案 1 :(得分:0)
当你这样做时:
jsonRootObject = new JSONObject(result);
您正在获取整个JSONobject字符串,因此您只需要获取类别对象:
jsonRootObject = jsonRootObject.getJSONObject("category")
然后你可以从类别中获取数组:
JSONArray jsonArray = jsonRootObject.getJSONArray("name");
答案 2 :(得分:0)
尝试像这样解析。这将为您提供所需的名称和brand_title
// Declare outside onCreate
JSONObject topObject;
JSONObject category;
JSONArray name;
String json;
JSONArray brand_title;
// the parsing
json = "{\"category\":{\"name\":[\"Spa\",\"Salon\",\"Makeup\"],\"brand_title\":[\"Aura Thai Spa- Greater Kailash 1\"]}}";
try {
topObject = new JSONObject(json);
} catch (JSONException e) {
e.printStackTrace();
}
try {
category = topObject.getJSONObject("category");
} catch (JSONException e) {
e.printStackTrace();
}
try {
name = category.getJSONArray("name");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i = 0; i<name.length(); i++){
try {
String parsedNanes = name.getString(i);
Log.i("parsedNames", parsedNanes);
} catch (JSONException e) {
e.printStackTrace();
}
}
try {
brand_title = category.getJSONArray("brand_title");
} catch (JSONException e) {
e.printStackTrace();
}
for(int i = 0; i<brand_title.length(); i++){
try {
String parsedBrandTitles = brand_title.getString(i);
Log.i("parsedBrandTitles", parsedBrandTitles);
} catch (JSONException e) {
e.printStackTrace();
}
}