2016-03-22 2 views
1

Ich lese ein JSON-Objekt aus Datei.wie Objekt innerhalb Objekt JSON mit Java zu aktualisieren und dann senden, um Payload Anfrage

ich in der Lage bin, den Wert zu lesen, aber wie kann ich den Codewert für meine Nutzlast aktualisieren

{ 
    "products": { 
     "productsApp15": { 
      "status": "active", 
      "attribute_set": "Apparel", 
      "name": "productsApp16", 
      "product_type": "product", 
      "code": "productsApp16" 
     } 
    } 
} 

Import Ich verwende: -

import java.io.FileNotFoundException; 
import java.io.FileReader; 
import java.io.IOException; 
import org.json.simple.JSONObject; 
import org.json.simple.parser.JSONParser; 
import org.json.simple.parser.ParseException; 
import java.util.Iterator; 
import java.io.FileWriter; 
import javax.json.JsonValue; 
import org.json.simple.JSONArray; 
import com.google.gson.Gson; 
import com.google.gson.GsonBuilder; 
import com.google.gson.JsonElement; 
import com.google.gson.JsonObject; 

Mein Code: -

// read the json file 
      FileReader reader = new FileReader(filePath); 

      JSONParser jsonParser = new JSONParser(); 
      JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); 

      JSONObject jsonObject1 = (JSONObject) jsonObject.get("products"); 
      JSONObject jsonObject2 = (JSONObject)jsonObject1.get("productsApp15"); 
      String firstName = (String) jsonObject2.get("code").toString(); 

      System.out.println("The first name is: " + firstName); 

Aber dieser Wert ändert nicht meine Anforderung Daten

Antwort

2

Below-Code für mich funktioniert: -

FileReader reader = new FileReader(filePath); 

    JSONParser jsonParser = new JSONParser(); 
    JSONObject jsonObject = (JSONObject) jsonParser.parse(reader); 

    JSONObject jsonObject1 = (JSONObject) jsonObject.get("products"); 
    JSONObject jsonObject2 = (JSONObject)jsonObject1.get("productsApp15"); 
    String firstName = (String) jsonObject2.get("code").toString(); 

    System.out.println("The first name is: " + firstName); 

    jsonObject2.remove("code"); 
    jsonObject2.put("code", "try"); 

    JSONObject jsonObject3 = (JSONObject)jsonObject1.get("productsApp15"); 
    String firstName2 = (String) jsonObject2.get("code").toString(); 
    System.out.println("The first name is: " + firstName2); 

Dank Rama Krishan

+0

wenn es funktioniert dann akzeptieren Sie es als Antwort @Subham Jain –

+0

Ja werde ich .. Ich kann meine eigene Antwort nicht vor 2 Tagen beantworten .. Danke Rama für Ihre Hilfe –

3

versuchen diese

JSONObject jsonObject1 = (JSONObject) jsonObject.get("products"); 
    JSONObject jsonObject2 = (JSONObject)jsonObject1.get("productsApp15"); 
    String firstName = (String) jsonObject2.get("code").toString(); 
+0

cool .. es funktioniert aber wie kann ich am selben Ort schreiben/aktualisieren? .. bedeutet, ich möchte diesen Wert für meine Nutzlast zu aktualisieren –

+0

Ich habe es und ich stelle es die ganze Lösung als Antwort hier .. Danke :) .. vote up –

+0

'öffentliche void replaceJson() löst JSONException { String json = "{\" products \ ": {\" productsApp15 \ ": {\" status \ ": \" active \ ", \" attribute_set \ ": \" Apparel \ ", \" name \ ": \" productsApp16 \ ", \ product_type \": \ "product \", \ "code \": \ "productsApp16 \"}}}; JSONObject jsonObject = neues JSONObject (json); JSONObject jsonObject1 = (JSONObject) jsonObject.get ("Produkte"); JSONObject jsonObject2 = (JSONObject) jsonObject1.get ("productsApp15"); jsonObject2.put ("code", "try"); System.out.println (jsonObject.toString()); } ' –

1

public void replaceJson() throws JSONException { String json = „{\ "Produkte \": {\ "ProdukteApp15 \": {\ "Status \": \ "active \", \ "attribute_set \": \ "Apparel \", \ "name \": \ "productsApp16 \", \ "Produkttyp": "Produkt", "Code": \ "ProdukteApp16 \"}}} ";

JSONObject jsonObject = new JSONObject(json); 
    JSONObject jsonObject1 = (JSONObject) jsonObject.get("products"); 
    JSONObject jsonObject2 = (JSONObject) jsonObject1.get("productsApp15"); 
     jsonObject2.put("code", "try"); 
      System.out.println(jsonObject.toString()); 
}' 
Verwandte Themen