1

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?

1
  • 1
    Where is your Java code? Commented Nov 8, 2012 at 9:32

5 Answers 5

2

Why reinvent the wheel, use GSON - A Java library that can be used to convert Java Objects into their JSON representation and vice-versa

Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

I've got: import net.sf.json.*;at the top with the other imports then your code, I get the errors Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException Caused by: java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException when trying to run it, What did I do wrong?
You must import the dependencies of the JSON-lib as well. It depends on Apache Commons too, so you got that error: > Json-lib requires (at least) the following dependencies in your classpath: > jakarta commons-lang 2.5 > jakarta commons-beanutils 1.8.0 > jakarta commons-collections 3.2.1 > jakarta commons-logging 1.1.1 > ezmorph 1.0.6
0

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

Comments

0

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) {

        }

   }

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.