在android中解析irregualr json格式的问题

时间:2016-03-04 12:01:47

标签: android json

我想解析以下JSON

{
"fail": true,
"errors": {
    "arrival_date": ["The arrival date should be greater than or equal to departure date !!!"]
   }
}

我希望获得的价值。到达日期应大于或等于出发日期!!!

我尝试了这段代码但没有成功

try {
    org.json.JSONObject jsonObj = new org.json.JSONObject(PostParseGet.mStringresponse);
    org.json.JSONObject   c = jsonObj.getJSONObject("errors");
    JSONArray contacts = c.getJSONArray("arrival_date");
    Log.d("Errors",contacts.get(0).toString());
} catch (JSONException e) {
    e.printStackTrace();
}

任何想法我怎样才能获得这个价值?

2 个答案:

答案 0 :(得分:2)

String data ="{ 'fail': 'true', 'errors': { 'arrival_date': ['The arrival date should be greater than or equal to departure date !!!']}}";
try {
    JSONObject object = new JSONObject(data);
    JSONObject error = object.getJSONObject("errors");
    JSONArray array = error.getJSONArray("arrival_date");
    String dataString  = array.getString(0);
    String dat = dataString;
}
catch(Exception e) {
    e.printStackTrace();
}

答案 1 :(得分:1)

尝试以下代码

try {
    JSONObject jObject=new JSONObject(PostParseGet.mStringresponse);

    JSONObject errorObject=jObject.optJSONObject("errors");
    JSONArray arraiValArray=errorObject.optJSONArray("arrival_date");

    if(arraiValArray.length()>0)
    {
        for (int i = 0; i < arraiValArray.length(); i++) {
            Log.i("TAG", "String:"+arraiValArray.optString(i));
        }
    }
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}