Java 如何在Android中格式化Wordpress中的长json文本

Java 如何在Android中格式化Wordpress中的长json文本,java,android,json,string,wordpress,Java,Android,Json,String,Wordpress,我以前问过这个问题,但没有得到一个有用的答案 我在我的Android应用程序中从Wordpress获得了一个很长的json文本,我想将其格式化为段落。由于文本具有类似于1,2,3的项目符号,-我需要文本以以下格式显示: 1 2 3 这就是我在json中获得它的方式: public class Didyouknow extends SherlockListActivity { private ActionBarMenu abm; private ProgressDialog pDialog;

我以前问过这个问题,但没有得到一个有用的答案

我在我的Android应用程序中从Wordpress获得了一个很长的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();
公共类Didyouknow扩展Sherlocklist活动{
私人行动;
私人对话;
//获取联系人JSON的URL
私有静态字符串url=“”;
//JSON节点名称
私有静态最终字符串标记_QUERY=“posts”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_TITLE=“TITLE”;
私有静态最终字符串标记_CONTENT=“CONTENT”;
//联系JSONArray
JSONArray查询=null;
//ListView的Hashmap
ArrayList查询列表;
@抑制警告(“弃用”)
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_);
ActionBar ActionBar=getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
abm=新操作栏菜单(Didyouknow.this);
if(com.cepfmobilepp.org.service.InternetStatus.getInstance(this.isOnline(this)){
//Toast t=Toast.makeText(这是“您在线!!!!”,8000).show();
//Toast.makeText(getBaseContext(),“您在线”,Toast.LENGTH_SHORT.show();
//调用异步任务以获取json
新建GetQuery().execute();
}否则{
AlertDialog NetAlert=new AlertDialog.Builder(Didyouknow.this.create();
setMessage(“未找到Internet连接!”);
NetAlert.setButton(“确定”,新的DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog,int which){
} });
NetAlert.show();
}
queryList=newarraylist();
ListView lv=getListView();
//单击项目上的Listview侦听器
lv.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父级、视图、,
内部位置,长id){
//从选定的ListItem获取值
//启动单一联系人活动
/*Intent in=新的Intent(getApplicationContext(),SingleContactActivity.class);
in.putExtra(标签名称、名称);
in.putExtra(标签、电子邮件、成本);
in.putExtra(标签、手机、说明);
星触觉(in)*/
}
});
}
私有字符串解析html(字符串html){
Document doc=Jsoup.parse(html);
返回doc.text();
}
私有类GetQuery扩展了异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=newprogressdialog(Didyouknow.this);
设置消息(“加载…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…arg0){
//创建服务处理程序类实例
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
query=jsonObj.getJSONArray(标记查询);
//通过所有触点循环
for(int i=0;ivalue
联系人。放置(标签号,ID);
联系人。放置(标签标题、标题);
put(TAG_CONTENT,parseHtml(CONTENT));
//联系人。放置(标签、手机、手机);
//将联系人添加到联系人列表
queryList.add(联系人);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
//关闭进度对话框
if(pDialog.isShowing())
pDialog.disclose();

由于您正在查看的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");
然后,您必须拆分[1]才能得到第2段和其他部分

String[] split2 = split[1].split("<p>3");
String paragraph2 = split2[0];
String[]split2=split[1]。split(3”);
字符串段落2=split2[0];
继续这样做,以获得所需的所有段落。
如果在每次拆分后都打印出每个字符串,那么就更容易掌握情况。

由于您的响应文本是html格式的,为什么不直接显示html?您可以发布要解析的JSON吗?还有,解析后的字符串应该显示在哪里?@JoakimPalmkvist,这是指向我正在尝试的JSON的链接但是,我不明白,请你用我的代码帮我回答这个问题,因为我是这方面的新手
paragraph1 = split[0].substring(3);
String[] split2 = split[1].split("<p>3");
String paragraph2 = split2[0];