从qjsonarray删除大括号并从文档中替换方括号

时间:2018-08-29 05:55:15

标签: c++ json qt qt5 qjsonobject

我正在这样向QJsonObjects插入很多不同的值:

//gender inserted to QJsonObject gender
QJsonObject gender;
gender.insert("gender", person->gender());

//birthDate inserted to QJsonObject birthDate
QJsonObject birthDate;
birthDate.insert("birthDate", person->birthdate().toString());

之后,我将QJsonObjects附加到QJsonArray,如下所示:

//Inserting all objects to QJsonDocument m_jsonDocument
QJsonArray allObjects;
allObjects.append(gender);
allObjects.append(birthDate);

然后将其全部放入QJsonDocument:

m_jsonDocument->setArray(allObjects);

输出:

[{
    "gender": "male"
},
{
    "birthDate": "2001-12-19"
}]

我需要除去对象周围的大括号,然后用大括号替换方括号。我是否需要将它们放入QString并删除和替换,或者是否有更简单的方法来修改对象,数组或整个文档?我试图四处张望,但尚未找到正确的解决方案。

这就是我希望看到的输出:

{
"gender": "male",
"birthDate": "2001-12-19"}

对象内部有很多东西,需要作为FHIR标准。对象内部有对象,文档仍然需要大量修改。

1 个答案:

答案 0 :(得分:0)

您应该只使用一个QJsonObject并将属性添加到该对象:

internal class KartListAdapter(private var arrayList: List<Kartdata>,
                           private val context: Context,
                           var presenter: MainContract.Presenter,
                           var recyclerview: RecyclerView,grandtotalTv:TextView) : RecyclerView.Adapter<KartListAdapter.MainHolder>() {

    var list=arrayList

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainHolder {
        val v = LayoutInflater.from(parent.context).inflate(R.layout.kart_newrow, parent, false)
        return MainHolder(v)
    }

    override fun onBindViewHolder(holder: MainHolder, position: Int) {

        holder.bindItem(arrayList[position])
        holder.itemsqty.tag = position
        holder.perpriceprice.tag=position

        var qtyval:Int = holder.itemsqty.text.toString().toInt()
        var perqtyPrice = arrayList[position].qtyPrice

        holder.btnIncrease.setOnClickListener() {
            qtyval++
            holder.itemsqty.text=qtyval.toString()
        }

        holder.btnDecrease.setOnClickListener() {
            qtyval--
            holder.itemsqty.text=qtyval.toString()
        }

        holder.itemsqty.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable) {
               // Toast.makeText(context,"after text change"+s.toString(),Toast.LENGTH_LONG).show()
            }

            override fun beforeTextChanged(s: CharSequence, start: Int,
                                           count: Int, after: Int) {
                //Toast.makeText(context,"before text change"+s.toString(),Toast.LENGTH_LONG).show()
            }

            override fun onTextChanged(s: CharSequence, start: Int,
                                       before: Int, count: Int) {
                if(s.toString().trim()!="") {
                    if (s.toString().toInt()<1) {
                        val perpiecetotal=qtyval*perqtyPrice
                        holder.itemsqty.text="1"
                        holder.perpriceprice.text="OMR:"+perpiecetotal.toString()

                    } else {
                        val perpiecetotal=qtyval*perqtyPrice
                        Log.i("pricing-qtyval",qtyval.toString())
                        Log.i("pricing-perqtyprice",perqtyPrice.toString())
                        holder.perpriceprice.text="OMR:"+perpiecetotal.toString()
                        Log.i("pricing-grandtotalprice",perpiecetotal.toString())
                        holder.perpriceprice.tag=position
                    }
                    //Toast.makeText(context,"on text change"+s.toString(),Toast.LENGTH_LONG).show()
                } else {
                    Toast.makeText(context,"somewhere qty is empty",Toast.LENGTH_LONG).show()

                }
            }
        })
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    override fun getItemCount(): Int {
        return arrayList.size
    }

    internal inner class MainHolder(view: View) : RecyclerView.ViewHolder(view) {

        var menuname = itemView.findViewById<TextView>(R.id.cateegory_tv)
        var qtyname = itemView.findViewById<TextView>(R.id.qty_name)
        var qtyprice = itemView.findViewById<TextView>(R.id.qty_price)
        var perpriceprice = itemView.findViewById<TextView>(R.id.price_calculated_tv_item)
        var itemsqty = itemView.findViewById<TextView>(R.id.no_of_items_edt)
        var deleteicon = itemView.findViewById<ImageView>(R.id.delete_icon)
        var btnIncrease = itemView.findViewById<ImageView>(R.id.btn_increase)
        var btnDecrease = itemView.findViewById<ImageView>(R.id.btn_decrease)

        fun bindItem(kart: Kartdata) {
            menuname.setText(kart.MenuName)
            qtyname.setText(kart.qtyName)
            qtyprice.setText("OMR\t"+kart.qtyPrice)
            val perpiecetotal=1*kart.qtyPrice
            perpriceprice.text="OMR:"+perpiecetotal.toString()

            deleteicon.setOnClickListener() {
                val database = KartDatabase.getInstance(context)
                val repository = KartRepository(AppExecutors(), database.dapendukDAO())
                Toast.makeText(context,"deleted",Toast.LENGTH_SHORT).show()
                presenter.deleteitem(repository,kart.id)
                presenter.getlistsize(repository)
                (context as FragmentActivity).supportFragmentManager.beginTransaction()
                        .disallowAddToBackStack()
                        .replace(R.id.container, KartFragment.newInstance(), KartFragment.TAG)
                        .commit()
            }
        }
    }
}

输出:

QJsonObject obj;
obj.insert("gender",  person->gender());
obj.insert("birthDate", person->birthdate().toString());
m_jsonDocument->setObject(obj);