0

次のオブジェクトを含む JavaScript ファイルがあります。

var MoceanSettings={
       BannerURL:'',
       IsDirectWall:true,
       AppOfDayZone:156431,
       ImpressTrackUrl:null,
       ClickTrackUrl:null,
       Categories:[
          {name:'App Gallery', Zone_T:165294,Zone_M:165295,Zone_B:165296},
          {name:'Entertainment', Zone_T:165306,Zone_M:165307,Zone_B:165308},
          {name:'Games', Zone_T:165297,Zone_M:165298,Zone_B:165299},
          {name:'Lifestyle', Zone_T:165309,Zone_M:165310,Zone_B:165311},
          {name:'Productivity', Zone_T:165303,Zone_M:165304,Zone_B:165305},
          {name:'Travel', Zone_T:165300,Zone_M:165301,Zone_B:165302},
          {name:'Favorites', Zone_T:156431,Zone_M:156431,Zone_B:156431}
       ]
}

Javaを使用して、このファイルをオブジェクト/配列に解析したいと思います。私は、JavaScript と json の実用的な知識が非常に限られています。

4

1 に答える 1

8

次のようなことができます。

import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

public class ExecuteScript {

    public static void main(String[] args) throws Exception {
        ScriptEngineManager factory = new ScriptEngineManager();
        ScriptEngine engine = factory.getEngineByName("JavaScript");
        engine.eval(new InputStreamReader(ExecuteScript.class.getResourceAsStream("javascript.js")));
    printJSElement("root", engine.get("MoceanSettings"));
    }

    private static void printJSElement(String propName, Object element) {
        if (element instanceof List) {
            printJSList(propName, (List<Object>) element);
        } else if (element instanceof Map) {
            printJSMap(propName, (Map<Object, Object>) element);
        } else {
            printJSProperty(propName, element);
        }
    }

    private static void printJSMap(String propName, Map<Object,Object> objectMap) {
        for (Entry<Object,Object> entry : objectMap.entrySet()) {
            printJSElement(propName + "." + String.valueOf(entry.getKey()), entry.getValue());
        }
    }

    private static void printJSList(String propName, List<Object> objectList) {
        for (int i = 0; i < objectList.size(); i++) {
            printJSElement(propName + "[" + i + "]", objectList.get(i));
        }
    }

    private static void printJSProperty(String propName, Object value) {
        System.out.println(propName + " = " + value);
    }

}

これにより、次の出力が表示されます。

root.BannerURL = 
root.IsDirectWall = true
root.AppOfDayZone = 156431
root.ImpressTrackUrl = null
root.ClickTrackUrl = null
root.Categories[0].name = App Gallery
root.Categories[0].Zone_T = 165294
root.Categories[0].Zone_M = 165295
root.Categories[0].Zone_B = 165296
root.Categories[1].name = Entertainment
root.Categories[1].Zone_T = 165306
root.Categories[1].Zone_M = 165307
root.Categories[1].Zone_B = 165308
root.Categories[2].name = Games
root.Categories[2].Zone_T = 165297
root.Categories[2].Zone_M = 165298
root.Categories[2].Zone_B = 165299
root.Categories[3].name = Lifestyle
root.Categories[3].Zone_T = 165309
root.Categories[3].Zone_M = 165310
root.Categories[3].Zone_B = 165311
root.Categories[4].name = Productivity
root.Categories[4].Zone_T = 165303
root.Categories[4].Zone_M = 165304
root.Categories[4].Zone_B = 165305
root.Categories[5].name = Travel
root.Categories[5].Zone_T = 165300
root.Categories[5].Zone_M = 165301
root.Categories[5].Zone_B = 165302
root.Categories[6].name = Favorites
root.Categories[6].Zone_T = 156431
root.Categories[6].Zone_M = 156431
root.Categories[6].Zone_B = 156431
于 2013-04-02T07:10:05.460 に答える