2

我搜索了很多资源,但找不到这个的答案..

众所周知,JSONObject 键以相反的顺序返回。

有没有办法以 JSON 中出现的正确顺序递归 JSONObject

String json;
JSONObject obj = new JSONObject(json)
Iterator<String> keys = json.keys()   ---> order is reversed

我知道 JSONObject 是无序的,也许有办法订购它????

JSON是下面的类型......并且键开始从最底部的styleHint标签开始递归

"sections": {


 "1": {
    "1": {
      "1": {
        "title": "xxx",
        "text": "xxx",
        "tags": {
          "audience": {
            "1": {
              "name": "xxx",
              "title": "xxx",
              "id": "xxx"
            }
          },
          "styleHint": {
            "1": {
              "name": "xxx",
              "title": "xxx",
              "id": "xxx"
            }
          }
        }
      },
      "title": "xxx",
      "text": "xxx",
      "tags": {
        "audience": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        },
        "styleHint": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        }
      }
    },
    "2": {
      "title": "xxx",
      "text": "xxx",
      "tags": {
        "audience": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        },
        "styleHint": {
          "1": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        }
      }
    },
    "title": "xxx",
    "text": "xxx",
    "tags": {
      "audience": {
        "1": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxx"
        },
        "2": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxx"
        }
      },
      "styleHint": {
        "1": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxx"
        }
      }
    }
  },
  "2": {
    "title": "xxx",
    "text": "xxx",
    "tags": {
      "audience": {
        "1": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxx"
        }
      },
      "styleHint": {
        "1": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxx"
        }
      }
    },
    "anchor":"xxx"

  },
  "3": {
    "1": {
      "title": "xxx",
      "text": "xxx",
      "tags": {
        "audience": {
          "tag": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        },
        "styleHint": {
          "tag": {
            "name": "xxx",
            "title": "xxx",
            "id": "xxx"
          }
        }
      }
    },
    "title": "xxx",
    "text": "xxx",
    "tags": {
      "audience": {
        "1": {
          "name": "xxx",
          "title": "xxx",
          "id": "xxxx"
        }
      },
      "styleHint": {
        "1": {
          "name": "xx",
          "title": "xxx",
          "id": "xxxx"
        }
      }
    }
  }  
}
4

1 回答 1

0

那个怎么样

获取您的 org.json.JSONObject 源并更改这些

public ListIterator keys() {
    ListIterator iter = new ArrayList(this.keySet()).listIterator();
    return iter;
}

/**
 * Construct an empty JSONObject.
 */
public JSONObject() {
    this.map = new LinkedHashMap();
}

/**
 * The map where the JSONObject's properties are kept.
 */
private final LinkedHashMap map;

public JSONObject(Map map) {
    this.map = new LinkedHashMap();
    if (map != null) {
        Iterator i = map.entrySet().iterator();
        while (i.hasNext()) {
            Map.Entry e = (Map.Entry) i.next();
            Object value = e.getValue();
            if (value != null) {
                this.map.put(e.getKey(), wrap(value));
            }
        }
    }
}

我希望我不会因此而在地狱中燃烧。

于 2014-01-26T07:09:05.917 回答