Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
String jsonString="{"name":"event_level_count","elements":[{"serial_number":"xxx12315","manufacturer_name":"xxx","count_level1":2004,"count_level2":1798,"count_level3":7},{"serial_number":"yyx01444","manufacturer_name":"xxx","count_level1":15,"count_level2":11,"count_level3":3}]}" ;
JSONObject output = new JSONObject(jsonString);
JSONArray docs = output.getJSONArray("elements");
System.out.println("Docs: "+docs);
Output Docs:
{"name":"event_level_count","elements":[{"count_level3":7,"count_level2":1798,"count_level1":2004,"manufacturer_name":"xxx","serial_number":"xxx12315"},{"count_level3":3,"count_level2":11,"count_level1":15,"manufacturer_name":"xxx","serial_number":"yyx01444"}]}
File file=new File("path \\ExportAsExcelfromJSON.csv");
String csv = CDL.toString(docs);
I need to reverse its as given string - jsonString.
JSON is simply a way to save or transfer data -- if you care about how it looks (e.g. how it's sorted), you are probably doing something wrong. If you want the array to be sorted in any way, do it once you have parsed the JSON back into actual data. Most programming languages (Java, for instance) come with easy-to-use tools to sort arrays.
As per Nizil's comment and this page, you can use the reverse(List<?> list) function from the Collections class in Java.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have a Stack Overflow question where I'm attempting to use Jython to extract a field value from JSON text:
Jython: Parse JSON object to get value (using Java functions)
A Stack Overflow community member has been kind enough to point me towards some Java documentation:
IBM >> Maximo >> Class JSONObject (Java)
Unfortunately, I've been staring at the Java documentation page for hours now, and to be honest, I have absolutely no idea what I'm looking at.
Where does this documentation show me how to extract a value from JSON text?
In other words, how do interpret this cryptic Java class documentation?
Start here, by passing the JSON string into the parse function.
Then, once you have your JSONObject, you can traverse the tree treating the object as a HashMap.
String jsonInput = "{ 'foo':'bar' }";
JSONObject jsonObject = JSONObject.parse(jsonInput);
String fooValue = jsonObject.get('foo');
Of course, this is the hard way. You might consider a more fluid library like JsonPath, which also has documentation that's more fluid.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a file called log.txt (in same directory as the program), and it contains data which I want to split based on . and store it into String[] plan.
e.g., The log.txt contains a string like 332 445.114 554.963 342. and so on...
What I want is to split it in such a way so that:
plan[0]=332 445;
plan[1]=114 554;
plan[2]=963 342;
And so on...
How about this:
String[] plan = (new Scanner( new File("log.txt") ).useDelimiter("\\A").next()).split("[\\r\\n]+");
This line saves lines from file into an array of String.
Is it okay for you?
Edit: Here is what you might be looking for...
String[] plan = (new Scanner( new File("log.txt") ).useDelimiter("\\A").next()).split("\\.");
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
<pko:POImport xmlns:xs="http://XMLSchema-instance"
xmlns:pko="hello"
xs:schemaLocation=" PlannedQuantityImport.xsd" tag="TAG" notes="These are my notes" totalqtypctmin="2" totalqtypctmax="3" mismatchthresholdpct="8.0">
<prodgrp id="100067">
<prodid>SYC87948427320</prodid>
<locgrp id="100067">
<geoid>30454</geoid>
<geoid>30982</geoid>
<quantity date="2010-12-11" sequence="1" prodid="87948427320">600</quantity>
</locgrp>
</prodgrp>
</pko:POImport>
I want to get the data corresponding to
tag, notes,totalqtypctmin,totalqtypctmax,mismatchthresholdpct,prodgrp,prodid,locgroup,geoid etc.
You will need XML parser to extract information from a XML using your desired xPath. Refer to this Best XML parser for Java.
I'd just use string matching.
inputStream = new BufferedReader(new FileReader("file.xml"));
while(inputStream.hasNext()){
String s=inputStream.next();
if(s.indexOf("totalqtypctmax")==-1)
continue;
String k = s.split("totalqtypctmax=\"")[1].split("\"")[0];
System.out.println("The value of totalqtypctmax is "+k);
}
Java isn't my first language, so pardon any silly mistakes.
EDIT: The only reason I'm using string matching instead of a standard XML parser, which is obviously recommended for more elaborate XML files, is because, in this case, your requirements seem very specific.
Their are various methods for parsing in java you could either use
DOM parsing
SAX parsing
These are included in the JDK by default
you can also use JDOM
Goto the following link to more info
http://www.mkyong.com/tutorials/java-xml-tutorials/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
{"category1":"value1","category2":"value2"}
Kindly do not downvote. I am asking this question here, to get direct answers and move on.
In android app I am trying to get the values or category1 and category 2 as follows, but I am getting errors. why ?
JSONParserPostGet jsonParser2 = new JSONParserPostGet();
JSONObject json = jsonParser2.makeHttpRequest(url, "POST", params);
if (json != null)
{
String category1 = json.getString("category1");
String category2 = json.getString("category2");
}
The code below just works fine. Please note that it is using org.json:
JSONObject object = new JSONObject("{\"category1\":\"value1\",\"category2\":\"value2\"}");
System.out.println(object.getString("category1"));
System.out.println(object.getString("category2"));
To my knowledge, there is no JSON parser in the Java standard library. Try using JSON in Java.
If your null pointer is on json, wrap it in a JSONException try catch and see if it gives you any more details.
You need to add JSON Library manually in your code as given by Joshua Swank and follow
http://www.mkyong.com/java/json-simple-example-read-and-write-json/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a text file which contains the follwing
{"sno":"1c8d1d7eaa32ff3f58a8822111276d5a","at":"app","tt":{"AppParam1":"AppParamValue1","AppParam2":"AppParamValue2"}},"c":{"dt":"Microsoft XDeviceEmulator","pn":"WP","pv":"8.0.9832.0"}
In java I want to find 'sno', 'pn' and 'pv' and replace the values of (what I mean is) currently the above text file has
"sno" has value "1c8d1d7eaa32ff3f58a8822111276d5a"
"pn" has value "WP"
"pv" has value "8.0.9832.0"
New values
"sno" has to be changed to "637829"
"pn" has to be changed to "XYZ"
"pv" has to be changed to "2.2.4.0"
Your help is much appreciated !
Thanks
That's a JSON object.
Convert that string to a JSON object, change the values and then convert it again to a string.
Here you can read about JSON objects in java: http://www.json.org/java/
And here an easy tuto on how to work with them: http://answers.oreilly.com/topic/257-how-to-parse-json-in-java/
You can convert it into JSON, make you changes and then convert it back to string.
Or if you really want to replace these values by yourself, you can use these lines :
yourstring = yourstring.replaceFirst("\"sno\":\"[^\"]+\"", "\"sno\":\"637829\"");
yourstring = yourstring.replaceFirst("\"pn\":\"[^\"]+\"", "\"pn\":\"XYZ\"");
yourstring = yourstring.replaceFirst("\"pv\":\"[^\"]+\"", "\"pv\":\"2.2.4.0\"");
Only if there are no way theses key can be found elsewhere in the string