简体   繁体   English

java解码json帮助(使用json-simple)

[英]java decode json help (using json-simple)

I know that asking for making something is bad but it's first time I'm stucked in java. 我知道要求做某事是不好的,但这是我第一次陷入Java。 Recently I began to learn java and for my small tool I need to read this json string. 最近,我开始学习Java,对于我的小型工具,我需要阅读此json字符串。

Half a day I was searching and trying to make things work and founded json-simple. 半天,我正在搜索并尝试使事情正常运行,并创建了json-simple。

But i cant do this. 但是我做不到。 Please help. 请帮忙。

For example just retrieve steamid and communityvisibilitystate to strings 例如,仅将Steamid和communityvisibilitystate检索为字符串

String steamid = ...;
String communityvisibilitystate = ...;

and if you have better solution not using json-simple post it 如果您有更好的解决方案不使用json-simple张贴

{
"response": {
    "players": [
        {
            "steamid": "76561198777777777",
            "communityvisibilitystate": 3,
            "profilestate": 1,
            "personaname": "777777",
            "lastlogoff": 7777777777,
            "profileurl": "http://steamcommunity.com/profiles/76561198777777777/",
            "avatar": "http://media.steampowered.com/steamcommunity/public/images/avatars/16/777777777.jpg",
            "avatarmedium": "http://media.steampowered.com/steamcommunity/public/images/avatars/16/777777777_medium.jpg",
            "avatarfull": "http://media.steampowered.com/steamcommunity/public/images/avatars/16/777777777_full.jpg",
            "personastate": 0,
            "primaryclanid": "103582797777777777",
            "timecreated": 7777777777
        }
    ]

}
}

1 : your complete response is a JSON OBJECT 1:您的完整响应是JSON对象

2 : if any element is written like 2:如果有任何类似的内容

"some key name " : { " some value " }

this is a JSON Object 这是一个JSON对象

3 : if any element is writen like 3:如果像这样写任何元素

 "some key name " :  " some value " 

this is value inside you json object which you can get by 这是您可以通过json对象获取的值

jsonObject.getString("key name")

4 : if any element is writen like 4:如果写入任何元素,例如

"some key name " : [ " some value " ]

then this is a JSON Array and you have to take it in to a JSON ARRAY and then traverse its elements by 那么这是一个JSON数组,您必须将其放入JSON ARRAY,然后遍历其元素

jsonObject.getJSONARRAY("key name for JSON ARRAY IN RESPONSE ")

and then you can traverse the elements of the JSON ARRAY by 然后您可以遍历JSON ARRAY的元素

`jsonArrayObj.get(0);`

After reading your Question .It seems you have 2 problem 看完您的问题之后,看来您有2个问题

To generate JSON online for that I would like to suggest you to Visit this Link . 要为此在线生成JSON,我建议您访问此链接 this will generate the required JSON what you want. 这将生成所需的所需JSON。

for parsing the json see the below codes suppose you have put this JSON response in String Variable result 对于解析json,请参见以下代码,假设您已将此JSON响应放入了String Variable结果中

            String result = "yourresponse";

            try {
                JSONObject object = new JSONObject(result);
                JSONObject object1 = object.getJSONObject("response");
                JSONArray arr = object1.getJSONArray("Players");
                for (int i = 0; i < arr.length(); i++) 
                {
                      JSONObject object3 = arr.getJSONObject(i);
                      String steamId = object3.getString("steamid");
                      String communityvisibilitystate = object3
                        .getString("communityvisibilitystate");
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

You can do sthing like this: 您可以执行以下操作:

Object obj = parser.parse(new FileReader("c:\\test.json"));

JSONObject jsonObject = (JSONObject) obj;

String steamid= (String) jsonObject.get("steamid");
System.out.println(steamid);

But i recommend using gson . 但是我建议使用gson It is much easier, and well documented. 这要容易得多,并且有据可查。

Create a class Result with the exact same structure as your json response (see below structure, for example) 使用与json响应完全相同的结构创建一个类Result(例如,参见下面的结构)

class Result {
    class Response {
        list of (<class Players> {
             String steamid
             String communityvisibilitystate
             String profilestate
             String personaname
             String lastlogoff
             String profileurl
             String avatar
             String avatarmedium
             String avatarfull
             String personastate
             String primaryclanid
             String timecreated
             })
       }
   }
}

Then use gson to parse your json like this. 然后使用gson这样解析您的json。

Result pareseJson = new Gson().fromJson(jsonString, Result.class);

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

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