こんにちは、Restful + JSON サーブレットを開発していますが、この方法でプロジェクト ソースをコンパイルしようとすると、奇妙な問題が発生します。
public
static
WipdDBTable
parseJSON(String JSONBody)
{
JSONObject jsonObj;
JSONTokener jsonTok;
Iterator it;
String[] labels;
String[][] fields;
int i;
try {
jsonTok = new JSONTokener(JSONBody);
jsonObj = new JSONObject(jsonTok);
labels = new String[jsonObj.length()];
fields = new String[1][labels.length];
i = 0;
it = jsonObj.keys();
while(it.hasNext()) {
String key = it.next().toString();
labels[i] = key;
fields[0][i] = jsonObj.get(key);
i++;
}
return new WipdDBTable(labels, fields);
} catch(JSONException ex) {
return null;
}
}
次のエラーが表示されます。
WipdJSON.java:102: incompatible types
found : java.lang.Object
required: java.lang.String
fields[0][i] = jsonObj.get(key);
したがって、明らかに同じソースを使用してテストクラスを作成しましたが、これではエラーは発生しません。
public class jsontest
{
public static
void
main(String[] args)
{
String s1;
JSONObject jsonObj;
JSONTokener jsonTok;
s1 = "{\"lo\":\"ol\",\"json\":{\"1\":\"2\"},\"yo\":{\"lol\":\"lol\"}}";
try {
jsonTok = new JSONTokener(s1);
jsonObj = new JSONObject(jsonTok);
Iterator it = jsonObj.keys();
while(it.hasNext()) {
String key = it.next().toString();
System.out.print(key + "=>");
System.out.println(jsonObj.get(key));
}
} catch(JSONException ex) {
ex.printStackTrace();
}
}
}