简体   繁体   中英

Convert Json Array to Java Array

I'm trying to convert this JSON string into an array:

{"result":"success","source":"chat","tag":null,"success":{"message":"%message%","time":%time%,"player":"%player%"}}

I would like to output it like this:

<%player%> %message%

I'm very new to java, I came from PHP where you could just do somthing along the lines of:

$result = json_decode($jsonfile, true);
echo "<".$result['success']['player']."> ".$result['success']['message'];

Output: <%player%> %message%

Is there an easy way to do this in java?

I searched for some similar topics but I didn't really understand them. Could someone explain this to me like I'm 5?

为什么重新发明轮子,使用GSON - 一个可用于将Java对象转换为JSON表示的Java库,反之亦然

JSON-lib is a good library for JSON in Java.

String jsonString = "{message:'%message%',player:'%player%'}";
JSONObject obj = JSONObject.fromObject(jsonString);    
System.out.println("<" + obj.get("message") + ">" + obj.get("player") );

You can also use xStream for doing it which has got a very simple API. Just Google for it.

You can always use the following libraries like:

- Jackson

- GSON

Ok here is the right way to do it Without using any library:

Eg:

JSONArray jarr = api.giveJsonArr();

// giveJsonArr() method is a custom method to give Json Array.

for (int i = 0; i < jarr.length(); i++) {

   JSONObject jobj = jarr.getJSONObject(i);   // Taking each Json Object

   String mainText = new String();            // fields to hold extracted data
   String provText = new String();
   String couText = new String();
   String fDatu = new String();

   try {
     mainText = jobj.getString("Overview");   // Getting the value of fields
     System.out.println(mainText);
    } catch (Exception ex) {

    }

    try {

    JSONObject jProv = jobj.getJSONObject("Provider");
    provText = jProv.getString("Name");
    System.out.println(provText);
    } catch (Exception ex) {

    }

    try {
        JSONObject jCou = jobj.getJSONObject("Counterparty");
        couText = jCou.getString("Value");
        System.out.println(couText);
    } catch (Exception ex) {

    }

    try {
                String cloText = jobj.getString("ClosingDate");
        fDatu = giveMeDate(cloText);
        System.out.println(fDatu);
        } catch (Exception ex) {

        }

   }

As you see you have many alternatives. Here is a simple one from json.org where you find lots of other alternatives. The one they supply them selves is simple. Here is your example:

    String json = "{\"result\":\"success\",\"source\":\"chat\",\"tag\":null,\"success\":{\"message\":\"%message%\",\"time\":%time%,\"player\":\"%player%\"}}";
    JSONObject obj = new JSONObject(json);
    JSONObject success = obj.getJSONObject("success");
    System.out.println("<" + success.get("player") + "> "
                       + success.get("message"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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