繁体   English   中英

如何在Java中解析json?

[英]How can I parse json in Java?

我有一个包含以下对象的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的使用知识非常有限。

您可以执行以下操作:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM