简体   繁体   English

如何获取特定的JSON数据?

[英]How to get specific JSON data?

Hi I am trying to read JSON from an ReST API but Im getting a nullpointer exception because mycode is not correct. 嗨,我正在尝试从ReST API读取JSON,但是由于mycode不正确,我收到了nullpointer异常。

I have a JSON that I am reading from looking like this : 我有一个从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"
    },

Now I want the name from the JSON document depending on what the user inputs. 现在,我想要JSON文档中的名称,具体取决于用户输入的内容。

how do I do this with code? 我该如何使用代码?

My code that is wrong is like this : 我的代码错误是这样的:

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");
                }
    }

I want get the same name of the location out from the JSON as the user inputs. 我想从JSON中获得与用户输入相同的位置名称。 How do I do this with code? 我该如何使用代码?

I think that you are asking how to parse your JSONObject and get the corresponding values out of it that the user is interested in. Below is an example of how you can pull apart the JSONObject to create a Map whose key is the String id (since the name does not seem to be unique) and whose value is the whole JSONObject . 我认为您正在询问如何解析JSONObject并从中获取用户感兴趣的相应值。下面是一个示例,您可以拆开JSONObject来创建一个键为String id的Map (因为名称似乎不是唯一的),其值是整个JSONObject You can use this map to lookup the input from the user and find the appropriate LLA if that's what you are interested in. 如果您对此感兴趣,则可以使用此映射查找用户的输入并找到适当的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;
}

You can tailor this method as you see fit. 您可以根据需要定制此方法。 For example if you are interested in the relationship between the id and the name then you can create a similar method that uses those values instead of id and the entire JSONObject' . 例如,如果您对idname之间的关系感兴趣,则可以创建一个使用这些值而不是id和整个JSONObject'的类似方法。 I hope that this helps~ 希望对您有所帮助〜

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

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