Blog/IT
[JAVA] ISO-8583 to JSON
IamwhatIam
2023. 12. 8. 12:29
ISO8583 메세지를 JSON으로 변환하는 코드가 필요해서 메모 목적으로 Blogging
일단 나는 Maven을 사용하니까... pom.xml 에 의존성 추가
<dependency>
<groupId>org.j8583</groupId>
<artifactId>j8583</artifactId>
<version>2.1.3</version> <!-- 최신 버전 확인 -->
</dependency>
Simple 하게 간단한 코드로 Sample을 메모...
import org.j8583.IsoMessage;
import org.j8583.IsoType;
import org.j8583.IsoValue;
import org.j8583.iso.IsoMessageFactory;
import org.j8583.iso.IsoTypeFactory;
import org.j8583.iso.Iso8583AsciiMessageFactory;
import java.io.IOException;
import java.text.ParseException;
import java.util.Map;
public class Iso8583ToJson {
public static void main(String[] args) throws IOException, ParseException {
// Example ISO 8583 message
byte[] isoMsgBytes = hexStringToByteArray("60000000006000000000000000000001481000605123456789012345654321");
// Create IsoMessageFactory
IsoMessageFactory<IMessage> messageFactory = new Iso8583AsciiMessageFactory();
messageFactory.setCharacterEncoding("UTF-8");
// Parse ISO 8583 message
IsoMessage isoMessage = messageFactory.parseMessage(isoMsgBytes, 0);
// Convert IsoMessage to JSON
Map<String, String> jsonMap = isoMessageToJson(isoMessage);
// Print JSON data
System.out.println(jsonMap);
}
private static Map<String, String> isoMessageToJson(IsoMessage isoMessage) {
// Convert IsoMessage to JSON
IsoValue<String> mtiValue = isoMessage.getField(0);
Map<String, String> jsonMap = Map.of(
"MTI", mtiValue.toString()
);
for (int i = 2; i <= 128; i++) { // Assuming fields range from 2 to 128
IsoValue<String> field = isoMessage.getField(i);
if (field != null && field.getType() == IsoType.ALPHA) {
jsonMap.put(Integer.toString(i), field.toString());
}
}
return jsonMap;
}
private static byte[] hexStringToByteArray(String hexString) {
int len = hexString.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4)
+ Character.digit(hexString.charAt(i + 1), 16));
}
return data;
}
}
가끔 은행에서 Project를 하다보면... 오래된 규격이기는 하지만 신용카드나 체크카드 같은 카드 시스템에서 사용되는
ISO8583 규격의 메세지를 JSON으로 변환해서 사용해야 하는 경우가 생긴다.
그런 경우에 사용하려고 메모해 둔다.