1

サーバーに http 応答を送信し、JSON 形式の文字列で応答を取得する Android アプリに取り組んでいます。この応答に基づいて、さまざまな機器に AChartEngine を使用して積み上げ縦棒グラフを生成する必要があります。応答文字列は次のようになります

[{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":0,"EquipmentDescription":"MTConnect","EquipmentId":5,"EquipmentName":"MTC 1","EquipmentStatusColor":null,"EquipmentStatusDescription":null,"EquipmentStatusId":0,"EquipmentStatusIdentifier":null,"EquipmentStatusTypeId":0,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":null,"EquipmentTimeZoneId":null,"EventData":null,"GeneralStatusState":0,"IsFirstRowData":false,"Percentage":0,"ReportGroupId":0,"ReportGroupName":null,"ReportGroupValue":null,"StatusAction":0,"StatusDuration":[{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":561.60722222222023,"EquipmentDescription":"MTConnect","EquipmentId":5,"EquipmentName":"MTC 1","EquipmentStatusColor":"#008000","EquipmentStatusDescription":"In Cycle","EquipmentStatusId":0,"EquipmentStatusIdentifier":null,"EquipmentStatusTypeId":1,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":"EquipmentStatusTypeName.InCycle","EquipmentTimeZoneId":null,"EventData":null,"GeneralStatusState":0,"IsFirstRowData":false,"Percentage":0,"ReportGroupId":0,"ReportGroupName":null,"ReportGroupValue":null,"StatusAction":0,"StatusDuration":null,"TimeStamp":{"DateTime":"\/Date(-62135596800000)\/","OffsetMinutes":0},"UnknownDowntime_Duration":0,"UnknownDowntime_Percent":0},{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":134.57583333333525,"EquipmentDescription":"MTConnect","EquipmentId":5,"EquipmentName":"MTC 1","EquipmentStatusColor":"#FFFF00","EquipmentStatusDescription":"In Cycle","EquipmentStatusId":0,"EquipmentStatusIdentifier":"","EquipmentStatusTypeId":2,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":"EquipmentStatusTypeName.UnknownDowntime","EquipmentTimeZoneId":null,"EventData":null,"GeneralStatusState":0,"IsFirstRowData":false,"Percentage":0,"ReportGroupId":0,"ReportGroupName":null,"ReportGroupValue":null,"StatusAction":0,"StatusDuration":null,"TimeStamp":{"DateTime":"\/Date(-62135596800000)\/","OffsetMinutes":0},"UnknownDowntime_Duration":0,"UnknownDowntime_Percent":0}],"TimeStamp":{"DateTime":"\/Date(-62135596800000)\/","OffsetMinutes":0},"UnknownDowntime_Duration":0,"UnknownDowntime_Percent":0},{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":0,"EquipmentDescription":null,"EquipmentId":1,"EquipmentName":"PCS Loop 1","EquipmentStatusColor":null,"EquipmentStatusDescription":null,"EquipmentStatusId":0,"EquipmentStatusIdentifier":null,"EquipmentStatusTypeId":0,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":null,"EquipmentTimeZoneId":null,"EventData":null,"GeneralStatusState":0,"IsFirstRowData":false,"Percentage":0,"ReportGroupId":0,"ReportGroupName":null,"ReportGroupValue":null,"StatusAction":0,"StatusDuration":[{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":495.61333333332925,"EquipmentDescription":null,"EquipmentId":1,"EquipmentName":"PCS Loop 1","EquipmentStatusColor":"#008000","EquipmentStatusDescription":"In Cycle","EquipmentStatusId":0,"EquipmentStatusIdentifier":null,"EquipmentStatusTypeId":1,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":"EquipmentStatusTypeName.InCycle","EquipmentTimeZoneId":null,"EventData":null,"GeneralStatusState":0,"IsFirstRowData":false,"Percentage":0,"ReportGroupId":0,"ReportGroupName":null,"ReportGroupValue":null,"StatusAction":0,"StatusDuration":null,"TimeStamp":{"DateTime":"\/Date(-62135596800000)\/","OffsetMinutes":0},"UnknownDowntime_Duration":0,"UnknownDowntime_Percent":0},{"ChartDate":null,"CycleStatus":null,"Cycle_Duration":0,"Cycle_Percent":0,"Duration":200.56972222222623,"EquipmentDescription":null,"EquipmentId":1,"EquipmentName":"PCS Loop 1","EquipmentStatusColor":"#FFFF00","EquipmentStatusDescription":"In Cycle","EquipmentStatusId":0,"EquipmentStatusIdentifier":"","EquipmentStatusTypeId":2,"EquipmentStatusTypeName":null,"EquipmentStatusTypeName_resourceKey":"EquipmentStatusTypeNam 

これは 5 つの異なる機器に対する応答です。この JSON 文字列を配列に使用して、各機器のデータを抽出し、グラフを描画するために AChartEngine に送信するにはどうすればよいですか?

4

3 に答える 3

1

以下のコードを使用して JsonArray を渡し、Value を ArrayList に格納します。

JSONArray jArray = new JSONArray(mRes);
ArrayList<String> array1 = new ArrayList<String>();
for(int i= 0; i< size; i++) {
    JSONObject jObject = jArray.getJSONObject(i);
    String mValue = jObject.getString("your_json_object_name");
    array1.add(mValue);
}
于 2012-09-19T07:50:13.250 に答える
1

応答文字列を JSONObject に変換できます。投稿した応答は JSONArray です。

     JSONArray jsonArray = new JSONArray(responseString);
     int size = jsonArray.length();
     ArrayList<JSONObject> arrayList = new ArrayList<JSONObject>();
     for(int i= 0; i< size; i++) {
          JSONObject jsonObject = jsonArray.optJSONObject(i);
          // use this jsonObject to get the value by passing the keys.
           String value = jsonObject.optString(key);

           //here is your array list of jsonobjects
           arrayList.add(jsonObject);
     }
于 2012-09-19T07:42:37.920 に答える
0

Gson api を使用すると、json オブジェクトを Java オブジェクトに直接マッピングできます。詳細については、次のリンクを確認してください。

http://code.google.com/p/google-gson/

https://sites.google.com/site/gson/gson-user-guide

于 2012-09-19T07:40:17.627 に答える