我之前曾问过这个问题,但没有得到有用的答案。
我从Wordpress的Android应用程序中获取了很长的json文本,我想对其进行格式化以包含段落。由于文本中包含1,2,3之类的项目符号,-我需要以以下格式显示文本:
1 ........
2 .......
3 .......
这就是我在json中获取它的方式:
public class Didyouknow extends SherlockListActivity {
private ActionBarMenu abm;
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "";
// JSON Node names
private static final String TAG_QUERY = "posts";
private static final String TAG_ID = "id";
private static final String TAG_TITLE = "title";
private static final String TAG_CONTENT = "content";
// contacts JSONArray
JSONArray query = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> queryList;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upcoming);
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
abm = new ActionBarMenu(Didyouknow.this);
if (com.cepfmobileapp.org.service.InternetStatus.getInstance(this).isOnline(this)) {
// Toast t = Toast.makeText(this,"You are online!!!!",8000).show();
// Toast.makeText(getBaseContext(),"You are online",Toast.LENGTH_SHORT).show();
// Calling async task to get json
new GetQuery().execute();
} else {
AlertDialog NetAlert = new AlertDialog.Builder(Didyouknow.this).create();
NetAlert.setMessage("No Internet Connection Found!");
NetAlert.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
} });
NetAlert.show();
}
queryList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
// Starting single contact activity
/*Intent in = new Intent(getApplicationContext(),SingleContactActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_EMAIL, cost);
in.putExtra(TAG_PHONE_MOBILE, description);
startActivity(in);*/
}
});
}
private String parseHtml(String html){
Document doc = Jsoup.parse(html);
return doc.text();
}
private class GetQuery extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Didyouknow.this);
pDialog.setMessage("Loading...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
query = jsonObj.getJSONArray(TAG_QUERY);
// looping through All Contacts
for (int i = 0; i < query.length(); i++) {
JSONObject c = query.getJSONObject(i);
String id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_CONTENT);
//String address = c.getString(TAG_ADDRESS);
//String gender = c.getString(TAG_GENDER);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_TITLE, title);
contact.put(TAG_CONTENT, parseHtml(content));
//contact.put(TAG_PHONE_MOBILE, mobile);
// adding contact to contact list
queryList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
由于您正在查看的JSON是hmtl,因此您可以将其显示在html页面中。
如果您想使用Java解析字符串,这里是如何实现的粗略草图:
更新:您的代码:
JSONObject c = query.getJSONObject(i);
String id = c.getString(TAG_ID);
String title = c.getString(TAG_TITLE);
String content = c.getString(TAG_CONTENT);
现在,字符串内容包含来自JSONObject的内容字符串。
String[] split = content.split("<p>2");
现在您有了一个字符串数组,
split1[0] = "<p>1)...."
split1[1] = ")content of p2 and p3 and so forth..."
因此,你需要从子[0],将删除该"<p>1"
如此
paragraph1 = split[0].substring(3);
那么您将不得不split split [1]来获得第2款,其余的
String[] split2 = split[1].split("<p>3");
String paragraph2 = split2[0];
继续执行此操作以获取所需的所有段落。如果在每次拆分后打印出每个String会更容易,那么您将更好地掌握正在发生的事情。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句