繁体   English   中英

如何获取特定的JSON数据?

[英]How to get specific JSON data?

嗨,我正在尝试从ReST API读取JSON,但是由于mycode不正确,我收到了nullpointer异常。

我有一个从JSON中读取的JSON,如下所示:

processJSON({
"LocationList":{
  "noNamespaceSchemaLocation":"http://api.vasttrafik.se/v1/hafasRestLocation.xsd",
  "servertime":"16:13",
  "serverdate":"2013-03-22",
  "StopLocation":[{
    "name":"Brunnsparken, Göteborg",
    "lon":"11.967824",
    "lat":"57.706944",
    "id":"9021014001760000",
    "idx":"1"
    },{
    "name":"Brunnsgatan, Göteborg",
    "lon":"11.959455",
    "lat":"57.693766",
    "id":"9021014001745000",
    "idx":"4"
    },{
    "name":"Brunnslyckan, Lerum",
    "lon":"12.410219",
    "lat":"57.812073",
    "id":"9021014017260000",
    "idx":"5"
    },

现在,我想要JSON文档中的名称,具体取决于用户输入的内容。

我该如何使用代码?

我的代码错误是这样的:

import org.json.simple.JSONObject;
import org.json.simple.JSONValue;

    public class JSONReader {

        private String jsonData = "";


        public String getJsonData(String location){


            try {

                    URL url = new     URL("http://api.vasttrafik.se/bin/rest.exe/v1/location.name?authKey=secret&format=json&jsonpCallback=processJSON&input=" + URLEncoder.encode(location, "UTF-8"));
                    URLConnection connection = url.openConnection();
                    BufferedReader readJsonFile = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
                    String temp = "";
                    while((temp = readJsonFile.readLine()) != null){
                            jsonData += temp;
                    }
                    readJsonFile.close();

                    System.out.println(jsonData);
                    return jsonData;

            }

            catch (IOException e) {

            }
            return null;
    }

       public void JSONParsing(){

                String location = Planner.getPlanner().getStartingLocation();
                JSONObject obj =(JSONObject)JSONValue.parse(getJsonData(location));

                //Set the text into the JList

                if (obj.containsValue(location));
                obj.get("name");
                }
    }

我想从JSON中获得与用户输入相同的位置名称。 我该如何使用代码?

我认为您正在询问如何解析JSONObject并从中获取用户感兴趣的相应值。下面是一个示例,您可以拆开JSONObject来创建一个键为String id的Map (因为名称似乎不是唯一的),其值是整个JSONObject 如果您对此感兴趣,则可以使用此映射查找用户的输入并找到适当的LLA。

public Map<String, JSONObject> createLocationMap(JSONObject jsonObj){
    Map<String, JSONObject> nameToLocationMap = new HashMap<String, JSONObject>();
    JSONObject locationList = (JSONObject) jsonObj.get("LocationList");
    JSONArray array = (JSONArray) locationList.get("StopLocation");
    for (int i = 0; i < array.length(); i++) {
        String name = (String) ((JSONObject) array.get(i)).get("id");
        nameToLocationMap.put(name, ((JSONObject)array.get(i)));
    }
    return nameToLocationMap;
}

您可以根据需要定制此方法。 例如,如果您对idname之间的关系感兴趣,则可以创建一个使用这些值而不是id和整个JSONObject'的类似方法。 希望对您有所帮助〜

暂无
暂无

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

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