【问题标题】:How to parse the same key name as string and object for JSON parsing?如何解析与 JSON 解析的字符串和对象相同的键名?
【发布时间】:2020-05-17 03:27:37
【问题描述】:

下面的响应值中的Hi是字符串,相同的值作为一个对象。如何解析android中这种情况的json

谁能帮我解决一下。

例如,pojo 类中的值 i 被定义为字符串,但在该循环失败后,因为在下一个值中是一个对象。

如何同时运行两个字符串和具有相同键名的对象

回复:

"fields": [
                            {
                                "name": "subject",
                                "value": "Meeting with Lead",
                                "label": "Subject",
                                "uitype": "2"
                            },
                            {
                                "name": "reminder_time",
                                "value": "0",
                                "label": "Send Reminder",
                                "uitype": "30"
                            },
                            {
                                "name": "assigned_user_id",
                                "value": {
                                    "value": "19x1",
                                    "label": "Administrator"
                                },
                                "label": "Assigned To",
                                "uitype": "53",
                                "type": {
                                    "defaultValue": {
                                        "value": "19x1",
                                        "label": "Administrator"
                                    }
                                }
                            },]

循环字段

private void fetchJSON(){

sessionId = getActivity().getIntent().getStringExtra("sessionId");
String operation = "syncModuleRecords";
String module = "Accounts";
String syncToken="";
String mode="public";
final GetNoticeDataService service = RetrofitInstance.getRetrofitInstance().create(GetNoticeDataService.class);

/** Call the method with parameter in the interface to get the notice data*/
Call<SyncModule> call = service.GetSyncModuleList(operation, sessionId, module,syncToken,mode);

/**Log the URL called*/
Log.i("URL Called", call.request().url() + "");

call.enqueue(new Callback<SyncModule>() {
    @Override
    public void onResponse(Call<SyncModule> call, Response<SyncModule> response) {

        Log.e("response", new Gson().toJson(response.body()));
        if (response.isSuccessful()) {
            Log.e("response", new Gson().toJson(response.body()));

            SyncModule syncModule = response.body();


            String success = syncModule.getSuccess();

            if (success.equals("true")) {
                SyncResults results = syncModule.getResult();

                Sync sync=results.getSync();

                ArrayList<SyncUpdated> syncUpdateds=sync.getUpdated();

                for(SyncUpdated syncUpdated:syncUpdateds){

                    ArrayList<SyncBlocks> syncBlocks=syncUpdated.getBlocks();

                    for(SyncBlocks syncBlocks1:syncBlocks){

                        String label=syncBlocks1.getLabel();

                        ArrayList<SynFields> synFields=syncBlocks1.getFields();

                        ArrayList<SynFields> jsonArray=syncBlocks1.getFields();


                        for(SynFields synFields1:synFields){
                            String name=synFields1.getName();
                            String value=synFields1.getValue();
                            String labelfield=synFields1.getLabel();

                            account_name.add(value);


                        }
                        myAdapter = new MyAdapter(getContext(),account_name);
                        recyclerView.setAdapter(myAdapter);
                        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));

                    }


                }

Pojo 类:

public class SynFields {

    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("value")
    @Expose
    private String value;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    @SerializedName("label")
    @Expose
    private String label;

}

【问题讨论】:

    标签: android json


    【解决方案1】:

    试试这个

    for(SynFields synFields1:synFields){
         String name=synFields1.getName();
         //String value=synFields1.getValue();
         String labelfield=synFields1.getLabel();
    
          Object values = synFields1.getValue();
    
          if (values instanceof JSONObject) {
             //here, you get jsonObject
              JSONObject jsonObject1 = new JSONObject(synFields1.getValue());
    
               // other stuff of   jsonObject1
    
          }else if (values instanceof String) {
               //here, you get string
               String your_string = synFields1.getValue();
          }
    
          account_name.add(value);
       }
    

    现在在你的 pojo 课上 替换

    @SerializedName("value")
        @Expose
        private String value;
    

    @SerializedName("value")
        @Expose
        private Object value;
    

    您还需要将 getter-settter 方法“值”更改为 Object

    希望这项工作:)

    【讨论】:

    • 尝试在 for 循环内添加它显示键错误
    • @jyothi 检查已编辑的答案。将其粘贴到 onResponse
    • @jyothi 检查已编辑的答案,如果您没有得到它或有任何错误,请告诉我
    • JSONObject jsonObject1 = new JSONObject(synFields1.getValue());在这一行?
    【解决方案2】:

    你可以通过这个简单地将 json 解析为 pojo 或 kotlin 类

    http://www.jsonschema2pojo.org/

    如果存在密钥,您可以轻松获得价值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-02-06
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 2019-11-27
      • 2013-03-12
      • 1970-01-01
      相关资源
      最近更新 更多