0

I am trying to create a simple JAVA remote for XBMC/KODI and I think im doing ok so far (still early days) but I have hit a snag when I reached a nested JSON object.

This is the original code I am converting to JAVA:

{"jsonrpc": "2.0", "method": "Player.PlayPause", "params": { "playerid": 0 }, "id": 1}

I have done this in JAVA so far:

public static void main(String[] args) throws UnknownHostException, IOException{
    JSONObject json = new JSONObject();
    json.put("jsonrpc", "2.0");
    json.put("method", "Player.PlayPause");
    //json.put("params", "playerid = 0"); THIS IS THE LINE I am having issues with
    Socket s = new Socket("192.168.0.21", 8080);
    try (OutputStreamWriter out = new OutputStreamWriter(s.getOutputStream(), StandardCharsets.UTF_8)) {
                out.write(json.toString());
    }}

As you can see from the original JSON there is a nested {} within the {} so {{}} and I dont know how to handle this. Im using JSON-Simple in eclipse if this helps, thanks for any help!

EDIT:

So that was helpful thanks, but it doesnt actually work is there anything wrong with the syntax:

public static void main(String[] args) throws UnknownHostException, IOException{
    JSONObject json = new JSONObject();
    JSONObject params = new JSONObject();
    json.put("jsonrpc", "2.0");
    json.put("method", "Player.PlayPause");
    params.put("playerid", 0);
    json.put("params", params);
    json.put("id", 1);
    Socket s = new Socket("192.168.0.21", 8080);
    try (OutputStreamWriter out = new OutputStreamWriter(s.getOutputStream(), StandardCharsets.UTF_8)) {
                out.write(json.toString());
    }
}
James McNee
  • 302
  • 2
  • 14

2 Answers2

1

Create another JSONObject for the params, set it up, and add it to the parent JSONObject with the key params.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • Hi I have updated my question with another question relating t the same thing, your answer helped me with the first one so thanks but could you please take a look at my further question? – James McNee Mar 03 '15 at 20:05
  • Hi, thanks for replying. I don't know what more I can say to be honest. No error messages are thrown and the programming starts and terminates. But the video does not pause. – James McNee Mar 05 '15 at 07:57
  • of course you can. has the problem anything to do with building a json object? or does it also happen if you directly use a correctly built json string? – Karoly Horvath Mar 05 '15 at 08:40
  • Yes it also happens when I directly input the string and escape the speech marks – James McNee Mar 05 '15 at 08:50
0

//import java.util.ArrayList;

//import org.bson.Document;

Document root= new Document();

Document rootParams = new Document();


root.append("jsonrpc","2.0");

root.append("method","Player.PlayPause");

rootParams.append("playerid",0);

root.append("id",1);




if (!rootParams.isEmpty()){
root.append("params",rootParams);
}


System.out.println(root.toJson());
Mateo
  • 10
  • 1