How to convert compact Json to pretty print codename one












1















I want to know how to convert a string of compact Json to pretty print so that I can parse it. I have searched for this question in stack overflow but it doesn't seem like anyone has asked it for codename one.
Right now I have a string of compact Json but it can not be parsed. This is the code:



String JsonData = "{"document":{ "type":"PLAIN_TEXT", "content":"Ask not what your country can do for you, ask what you can do for your country." },"encodingType":"UTF8"}";
JsonResponse = Rest.
post("https://language.googleapis.com/v1/documents:analyzeSyntax?key=[API KEY").
jsonContent().
body(JsonData).
getAsJsonMap();
String JsonString = (JsonResponse.getResponseData()).toString();
JSONParser parser = new JSONParser();
Map<String, Object> results = null;
try {
results = parser.parseJSON(new StringReader(JsonString));
} catch (IOException e) {
e.printStackTrace();
System.out.println("fail");
}
System.out.println("results "+results);


But when I run this code I get a bunch of these responses:



[EDT] 0:0:3,269 - Expected null for key value while parsing JSON token at row: 1 column: 5 buffer: e


and



java.lang.NumberFormatException: For input string: "ee0"


How should I convert my string of compact Json (JsonString) to pretty print so that I can parse it? Alternatively, is there a way to directly parse the response (JsonResponse)?
Thank You










share|improve this question

























  • could you post your full code? this obviously doesn't compile.

    – cen0r
    Jan 18 at 23:47













  • My program is pretty long but this is a free standing part: nothing else in the program affects this request.

    – Max Litvak
    Jan 19 at 1:56
















1















I want to know how to convert a string of compact Json to pretty print so that I can parse it. I have searched for this question in stack overflow but it doesn't seem like anyone has asked it for codename one.
Right now I have a string of compact Json but it can not be parsed. This is the code:



String JsonData = "{"document":{ "type":"PLAIN_TEXT", "content":"Ask not what your country can do for you, ask what you can do for your country." },"encodingType":"UTF8"}";
JsonResponse = Rest.
post("https://language.googleapis.com/v1/documents:analyzeSyntax?key=[API KEY").
jsonContent().
body(JsonData).
getAsJsonMap();
String JsonString = (JsonResponse.getResponseData()).toString();
JSONParser parser = new JSONParser();
Map<String, Object> results = null;
try {
results = parser.parseJSON(new StringReader(JsonString));
} catch (IOException e) {
e.printStackTrace();
System.out.println("fail");
}
System.out.println("results "+results);


But when I run this code I get a bunch of these responses:



[EDT] 0:0:3,269 - Expected null for key value while parsing JSON token at row: 1 column: 5 buffer: e


and



java.lang.NumberFormatException: For input string: "ee0"


How should I convert my string of compact Json (JsonString) to pretty print so that I can parse it? Alternatively, is there a way to directly parse the response (JsonResponse)?
Thank You










share|improve this question

























  • could you post your full code? this obviously doesn't compile.

    – cen0r
    Jan 18 at 23:47













  • My program is pretty long but this is a free standing part: nothing else in the program affects this request.

    – Max Litvak
    Jan 19 at 1:56














1












1








1


1






I want to know how to convert a string of compact Json to pretty print so that I can parse it. I have searched for this question in stack overflow but it doesn't seem like anyone has asked it for codename one.
Right now I have a string of compact Json but it can not be parsed. This is the code:



String JsonData = "{"document":{ "type":"PLAIN_TEXT", "content":"Ask not what your country can do for you, ask what you can do for your country." },"encodingType":"UTF8"}";
JsonResponse = Rest.
post("https://language.googleapis.com/v1/documents:analyzeSyntax?key=[API KEY").
jsonContent().
body(JsonData).
getAsJsonMap();
String JsonString = (JsonResponse.getResponseData()).toString();
JSONParser parser = new JSONParser();
Map<String, Object> results = null;
try {
results = parser.parseJSON(new StringReader(JsonString));
} catch (IOException e) {
e.printStackTrace();
System.out.println("fail");
}
System.out.println("results "+results);


But when I run this code I get a bunch of these responses:



[EDT] 0:0:3,269 - Expected null for key value while parsing JSON token at row: 1 column: 5 buffer: e


and



java.lang.NumberFormatException: For input string: "ee0"


How should I convert my string of compact Json (JsonString) to pretty print so that I can parse it? Alternatively, is there a way to directly parse the response (JsonResponse)?
Thank You










share|improve this question
















I want to know how to convert a string of compact Json to pretty print so that I can parse it. I have searched for this question in stack overflow but it doesn't seem like anyone has asked it for codename one.
Right now I have a string of compact Json but it can not be parsed. This is the code:



String JsonData = "{"document":{ "type":"PLAIN_TEXT", "content":"Ask not what your country can do for you, ask what you can do for your country." },"encodingType":"UTF8"}";
JsonResponse = Rest.
post("https://language.googleapis.com/v1/documents:analyzeSyntax?key=[API KEY").
jsonContent().
body(JsonData).
getAsJsonMap();
String JsonString = (JsonResponse.getResponseData()).toString();
JSONParser parser = new JSONParser();
Map<String, Object> results = null;
try {
results = parser.parseJSON(new StringReader(JsonString));
} catch (IOException e) {
e.printStackTrace();
System.out.println("fail");
}
System.out.println("results "+results);


But when I run this code I get a bunch of these responses:



[EDT] 0:0:3,269 - Expected null for key value while parsing JSON token at row: 1 column: 5 buffer: e


and



java.lang.NumberFormatException: For input string: "ee0"


How should I convert my string of compact Json (JsonString) to pretty print so that I can parse it? Alternatively, is there a way to directly parse the response (JsonResponse)?
Thank You







json codenameone pretty-print






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 18 at 23:44







Max Litvak

















asked Jan 18 at 23:29









Max LitvakMax Litvak

183




183













  • could you post your full code? this obviously doesn't compile.

    – cen0r
    Jan 18 at 23:47













  • My program is pretty long but this is a free standing part: nothing else in the program affects this request.

    – Max Litvak
    Jan 19 at 1:56



















  • could you post your full code? this obviously doesn't compile.

    – cen0r
    Jan 18 at 23:47













  • My program is pretty long but this is a free standing part: nothing else in the program affects this request.

    – Max Litvak
    Jan 19 at 1:56

















could you post your full code? this obviously doesn't compile.

– cen0r
Jan 18 at 23:47







could you post your full code? this obviously doesn't compile.

– cen0r
Jan 18 at 23:47















My program is pretty long but this is a free standing part: nothing else in the program affects this request.

– Max Litvak
Jan 19 at 1:56





My program is pretty long but this is a free standing part: nothing else in the program affects this request.

– Max Litvak
Jan 19 at 1:56












1 Answer
1






active

oldest

votes


















0














You are printing out a map not a JSON string as the JSON data is already parsed. If you just want to look at the network protocol for debugging the best way to do that is open the Network Monitor in the simulator where you will see all HTTP requests and can copy out the response body JSON.



However you can still convert a Map back to JSON using:



Log.p("results   " + JSONParser.mapToJson(results));


Notice you should use Log.p() and Log.e() to log strings/exceptions as that would work better on the devices.






share|improve this answer
























  • Thank you. That line of code was helpful. I think that I was asking the wrong questions though. I wanted to put the response into a Map<String, Object> and I did not realize that you could just do Response = Map<String, Object>.

    – Max Litvak
    Jan 19 at 13:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54262708%2fhow-to-convert-compact-json-to-pretty-print-codename-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You are printing out a map not a JSON string as the JSON data is already parsed. If you just want to look at the network protocol for debugging the best way to do that is open the Network Monitor in the simulator where you will see all HTTP requests and can copy out the response body JSON.



However you can still convert a Map back to JSON using:



Log.p("results   " + JSONParser.mapToJson(results));


Notice you should use Log.p() and Log.e() to log strings/exceptions as that would work better on the devices.






share|improve this answer
























  • Thank you. That line of code was helpful. I think that I was asking the wrong questions though. I wanted to put the response into a Map<String, Object> and I did not realize that you could just do Response = Map<String, Object>.

    – Max Litvak
    Jan 19 at 13:19
















0














You are printing out a map not a JSON string as the JSON data is already parsed. If you just want to look at the network protocol for debugging the best way to do that is open the Network Monitor in the simulator where you will see all HTTP requests and can copy out the response body JSON.



However you can still convert a Map back to JSON using:



Log.p("results   " + JSONParser.mapToJson(results));


Notice you should use Log.p() and Log.e() to log strings/exceptions as that would work better on the devices.






share|improve this answer
























  • Thank you. That line of code was helpful. I think that I was asking the wrong questions though. I wanted to put the response into a Map<String, Object> and I did not realize that you could just do Response = Map<String, Object>.

    – Max Litvak
    Jan 19 at 13:19














0












0








0







You are printing out a map not a JSON string as the JSON data is already parsed. If you just want to look at the network protocol for debugging the best way to do that is open the Network Monitor in the simulator where you will see all HTTP requests and can copy out the response body JSON.



However you can still convert a Map back to JSON using:



Log.p("results   " + JSONParser.mapToJson(results));


Notice you should use Log.p() and Log.e() to log strings/exceptions as that would work better on the devices.






share|improve this answer













You are printing out a map not a JSON string as the JSON data is already parsed. If you just want to look at the network protocol for debugging the best way to do that is open the Network Monitor in the simulator where you will see all HTTP requests and can copy out the response body JSON.



However you can still convert a Map back to JSON using:



Log.p("results   " + JSONParser.mapToJson(results));


Notice you should use Log.p() and Log.e() to log strings/exceptions as that would work better on the devices.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 19 at 5:41









Shai AlmogShai Almog

40.3k52555




40.3k52555













  • Thank you. That line of code was helpful. I think that I was asking the wrong questions though. I wanted to put the response into a Map<String, Object> and I did not realize that you could just do Response = Map<String, Object>.

    – Max Litvak
    Jan 19 at 13:19



















  • Thank you. That line of code was helpful. I think that I was asking the wrong questions though. I wanted to put the response into a Map<String, Object> and I did not realize that you could just do Response = Map<String, Object>.

    – Max Litvak
    Jan 19 at 13:19

















Thank you. That line of code was helpful. I think that I was asking the wrong questions though. I wanted to put the response into a Map<String, Object> and I did not realize that you could just do Response = Map<String, Object>.

– Max Litvak
Jan 19 at 13:19





Thank you. That line of code was helpful. I think that I was asking the wrong questions though. I wanted to put the response into a Map<String, Object> and I did not realize that you could just do Response = Map<String, Object>.

– Max Litvak
Jan 19 at 13:19


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54262708%2fhow-to-convert-compact-json-to-pretty-print-codename-one%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Homophylophilia

Updating UILabel text programmatically using a function

Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage