【问题标题】:How to remove and parse the empty string from json object in android and display as list如何从android中的json对象中删除和解析空字符串并显示为列表
【发布时间】:2017-06-14 06:06:22
【问题描述】:

我使用复选框来选择字段,如下图

我得到 json 对象,

"painWorse":"Sitting,Standing,,,,Lying Down"

我需要解析并显示为

  • 坐着
  • 站立
  • 躺下

目前我将文本设置为:

viewHolder.painWorse.setText(assessObject.get("painWorse").toString().replace("\"", ""));

我需要跳过空项目,只解析和显示选中的项目。如何实现这一点

适配器代码:

public class AssessmentAdapter extends RealmBasedRecyclerViewAdapter<Assessment, AssessmentAdapter.ViewHolder> {

    Context mContext;

    public AssessmentAdapter(
            Context context,
            RealmResults<Assessment> realmResults,
            boolean automaticUpdate,
            boolean animateIdType) {
        super(context, realmResults, automaticUpdate, animateIdType);
        mContext = context;
    }

    public class ViewHolder extends RealmViewHolder {

//

        @BindView(R.id.visitDate)
        TextView visitDate;
        @BindView(R.id.Name)
        TextView name;
        @BindView(R.id.txtAge)
        TextView age;
        @BindView(R.id.txtSex)
        TextView sex;
        @BindView(R.id.problemBegin)
        TextView problem;
        @BindView(R.id.problem)
        TextView problemBegin;
        @BindView(R.id.morningRate)
        TextView morningRate;
        @BindView(R.id.afternoonRate)
        TextView afternoonRate;
        @BindView(R.id.eveningRate)
        TextView eveningRate;
        @BindView(R.id.assessmentDetails)
        CardView assessmentDetails;
        @BindView(R.id.painWorse)
        TextView painWorse;
        @BindView(R.id.currentCondition)
        TextView currentCondition;
        @BindView(R.id.diagnosticStudied)
        TextView diagnosticStudied;
        @BindView(R.id.medications)
        TextView medications;
        @BindView(R.id.history)
        TextView history;
        @BindView(R.id.goals)
        TextView goals;


        public ViewHolder(View container) {
            super(container);
            ButterKnife.bind(this, container);
        }
    }

    @Override
    public ViewHolder onCreateRealmViewHolder(ViewGroup viewGroup, int viewType) {

        ViewHolder vh = null;
        try {
            View v = inflater.inflate(R.layout.assessment_card_view, viewGroup, false);
            vh = new ViewHolder(v);

        } catch (Exception e) {
            Log.v(Constants.TAG, "onCreateRealmViewHolder Exception: " + e.toString());
        }
        return vh;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onBindRealmViewHolder(ViewHolder viewHolder, int position) {

        final Assessment assessment = realmResults.get(position);

        try {


            JsonParser parser = new JsonParser();
            JsonArray assess = parser.parse(assessment.getAssesment().toString()).getAsJsonArray();
            Log.v(Constants.TAG, "assessing" + assess);
            JsonObject assessObject = assess.get(0).getAsJsonObject();
            Log.v(Constants.TAG, "assessObject" + assessObject);
            viewHolder.visitDate.setText(assessObject.get("Date").toString().replace("\"", ""));
            viewHolder.name.setText(assessObject.get("Name").toString().replace("\"", ""));
            viewHolder.age.setText(assessObject.get("Age").toString().replace("\"", ""));
            viewHolder.sex.setText(assessObject.get("Sex").toString().replace("\"", ""));
            viewHolder.problem.setText(assessObject.get("Problem").toString().replace("\"", ""));
            viewHolder.problemBegin.setText(assessObject.get("ProblemBegin").toString().replace("\"", ""));
            viewHolder.morningRate.setText(assessObject.get("morningRate").toString().replace("\"", ""));
            viewHolder.afternoonRate.setText(assessObject.get("afternoonRate").toString().replace("\"", ""));
            viewHolder.eveningRate.setText(assessObject.get("eveningRate").toString().replace("\"", ""));
            viewHolder.painWorse.setText(assessObject.get("painWorse").toString().replace("\"", ""));
            viewHolder.currentCondition.setText(assessObject.get("currentCondition").toString().replace("\"", ""));
            viewHolder.diagnosticStudied.setText(assessObject.get("Diagnostic").toString().replace("\"", ""));
            viewHolder.medications.setText(assessObject.get("Medications").toString().replace("\"", ""));
            viewHolder.history.setText(assessObject.get("History").toString().replace("\"", ""));
            viewHolder.goals.setText(assessObject.get("Goals").toString().replace("\"", ""));
            viewHolder.assessmentDetails.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                }
            });


        } catch (Exception e) {
            Log.v(Constants.TAG, "Exception: " + e.toString());
        }

    }


}

【问题讨论】:

  • 你能分享你试图解析的代码吗?什么行动或事件?
  • viewHolder.painWorse.setText(assessObject.get("painWorse").toString().replace("\"", ""));
  • 这里我正在解析 painWorse 对象的内容。我得到的输出是 Sitting,Standing,,,,Lying Down 。我需要删除空字符串
  • 我已经更新了代码,请检查

标签: android json android-studio string-parsing


【解决方案1】:

在不了解您的代码的情况下,我会说您需要使用正则表达式来删除这些多个逗号

String pain = "Sitting,Standing,,,,Lying Down";
pain = pain.replaceAll(",+",","); // Replace every ocurrence of one or more commas with just one comma
// pain = "Sitting,Standing,Lying Down";

格式化此信息的更清晰和更简单的方法是使用LinearLayout 动态填充与项目一样多的TextViews。因此,一旦您可以执行以下操作:

  1. 将您的painWorse 更改为垂直方向的LinearLayout
  2. 将您的字符串分成几个项目String[] items = pain.split(",");
  3. 动态填写您的新LinearLayout

    for (String item : items) {
        TextView tv = new TextView(context);
        tv.setText(item);
        // any other styling here...
    }
    

【讨论】:

  • 很好,我正在坐、站、躺。如何将这些内容显示为列表
  • 如果你想把一个字符串分成几个项目,你应该检查一些other questions
  • 谢谢兄弟,我用 viewHolder.painWorse.setText( " \u2022 " + TextUtils.join("\n \u2022 ", pain.toString().replace("\"", " ").replace("[", "").replace("]", "").split(",")));
  • 对我来说似乎是一种解决方法。考虑使用LinearLayout 而不是painWorse TextView。这样,您可以在运行时根据需要动态添加 TextViews(甚至以更好、更清晰的方式对其进行格式化)
  • 不客气!作为一般规则,认为“作为用户看起来不错”与“作为需要维护代码的开发人员看起来不错”不同。所以尽量实现这两个目标;)
【解决方案2】:

您可以使用GSON/Jackson 来解析您的 JSON 对象。在这些库中,您会发现很多注释可以排除 EMPTY、NULL 等。它们还提供了更易于阅读的代码

这里给出了一个使用 GSON 的例子

https://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/

【讨论】:

  • 好的,会检查的。谢谢
  • 还没有,兄弟。我需要仅将选中的项目显示为列表
猜你喜欢
  • 1970-01-01
  • 2016-04-27
  • 2017-05-08
  • 1970-01-01
  • 2011-10-15
  • 2011-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多