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 回答